【问题标题】:Regarding Configure AAD Integration using ASPNet Core 2.1 Web API关于使用 ASPNet Core 2.1 Web API 配置 AAD 集成
【发布时间】:2018-11-01 14:50:21
【问题描述】:

我已经有一个使用 ASP.Net Core 2.1 的 AAD 集成 WebAPP,但现在我想使用 ASPNet Core 2.1 开发一个 API,以使用 JWT 不记名令牌向我的 api 验证 AAD 用户。我无法执行相同的操作,因为在 Web 应用程序中我使用的是 Cookie Auth 模式,但在这里我需要实现对我不起作用的 JWT Bearer。我尝试了很多来自不同代码仓库的代码。

参考资料: https://github.com/juunas11/Joonasw.AzureAdApiSample

https://github.com/Azure-Samples/active-directory-b2c-dotnetcore-webapi

https://azure.microsoft.com/en-in/resources/samples/active-directory-dotnet-native-aspnetcore-v2/

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Audience = "https://localhost:44395/";
options.Authority = "https://localhost:44395/identity/";
})
.AddJwtBearer("AzureAD", options =>
{
options.Audience = "https://localhost:44395/";
options.Authority = "https://login.microsoftonline.com/tenantID/";
});

        services.AddAuthorization(options =>
        {
            var defaultAuthorizationPolicyBuilder = new AuthorizationPolicyBuilder(
                JwtBearerDefaults.AuthenticationScheme,
                "AzureAD");
            defaultAuthorizationPolicyBuilder =
                defaultAuthorizationPolicyBuilder.RequireAuthenticatedUser();
            options.DefaultPolicy = defaultAuthorizationPolicyBuilder.Build();
        });

当我将模式更改为 Cookie 模式时,它可以正常工作,但不能在 JWTBearer 代码中工作。

Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:Information: Authorization failed.
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
Microsoft.AspNetCore.Mvc.ChallengeResult:Information: Executing ChallengeResult with authentication schemes (Bearer, AzureAD).
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler:Information: AuthenticationScheme: Bearer was challenged.
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler:Information: AuthenticationScheme: AzureAD was challenged.

有人可以帮我吗?由于这个问题,我被困在这里。

提前致谢

【问题讨论】:

    标签: azure asp.net-core


    【解决方案1】:

    假设您有用 ASP.NET Core Web API 编写并受 Azure AD 保护的 REST API 资源,客户端(ASP.NET Web 应用程序)可以使用 OpenID Connect 中间件和 Active Directory 身份验证库 (ADAL.NET)使用 OAuth 2.0 协议为已登录用户获取 JWT 不记名令牌。

    不记名令牌被传递给Web API,Web API 使用JWT 不记名身份验证中间件验证令牌并授权用户,例如,参考第一个链接中的代码示例:

            services
            .AddAuthentication(o =>
            {
                o.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(o =>
            {
                o.Authority = Configuration["Authentication:Authority"];
                o.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
                {
                    // Both App ID URI and client id are valid audiences in the access token
                    ValidAudiences = new List<string>
                    {
                        Configuration["Authentication:AppIdUri"],
                        Configuration["Authentication:ClientId"]
                    }
                };
            });
    

    以上代码示例使用 JwtBearerExtensions 来验证访问令牌。您可以单击here 了解有关场景的说明。

    【讨论】:

      猜你喜欢
      • 2020-06-20
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 2019-05-06
      • 2021-11-08
      相关资源
      最近更新 更多