【问题标题】:Mixed Authentication in ASP.NET Core (Windows and AAD JwtBearer)ASP.NET Core 中的混合身份验证(Windows 和 AAD JwtBearer)
【发布时间】:2021-11-01 01:42:08
【问题描述】:

我正在开发应该同时具有 Windows 和 Azure Active Directory JwtBearer 身份验证的 .Net 5 Web 应用程序。一个控制器将使用一种 Windows 身份验证,而另一种身份验证将用于其他控制器。很清楚如何设置一个或另一个,但我不确定如何设置它们。

对于 Windows 身份验证:

services.AddAuthentication(IISDefaults.AuthenticationScheme);

对于 Azure Active Directory JwtBearer 身份验证:

services.AddAuthentication(AzureADDefaults.JwtBearerAuthenticationScheme)
      .AddAzureADBearer(options => Configuration.Bind("AzureAd", options));

似乎我无法在 AddAuthentication 方法中设置多个身份验证,我想知道我们是否可以同时进行多个身份验证?我想以某种方式在 Startup.cs 中设置它们,并具有带有身份验证方案的控制器属性。

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class JwtBearerProtectedController : ControllerBase
...
[Authorize(AuthenticationSchemes = IISDefaults.AuthenticationScheme)]
public class WindowsAuthProtectedController : ControllerBase

更新: 通过反复试验,我发现启动配置适用于 Windows 身份验证和 AAD 不记名身份验证。

JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
IdentityModelEventSource.ShowPII = true;

services
   .AddAuthentication(IISDefaults.AuthenticationScheme)
   .AddAzureAdBearer(options => Configuration.Bind("AzureAd", options))

【问题讨论】:

  • 我目前在我的一个项目中使用 3 种身份验证方案(1 我自己定制,一个用于 Jwt 标准系统,一个用于在 node-js 上的另一个系统上手动生成 jwt)。所以答案很简单,是的,我们可以,而且有效。
  • 下面提供的文档是否解决了您的问题?如果不是,您面临什么问题?

标签: c# asp.net-core authentication jwt windows-authentication


【解决方案1】:

是的,您绝对可以有多个身份验证选项

我想将您引导至此 msft 文档

https://docs.microsoft.com/en-us/aspnet/core/security/authorization/limitingidentitybyscheme?view=aspnetcore-5.0

【讨论】:

    猜你喜欢
    • 2020-04-25
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    相关资源
    最近更新 更多