首页 > 解决方案 > 关于使用 ASPNet Core 2.1 Web API 配置 AAD 集成

问题描述

我已经有一个使用 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.

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

提前致谢

标签: azureasp.net-core

解决方案


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

不记名令牌被传递给 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 来验证访问令牌。您可以单击此处了解有关场景的说明。


推荐阅读