angular - Azure AD 2.0 应用程序 - 无法从 Angular 客户端访问范围

标签 angular asp.net-core azure-active-directory

我正在尝试开发 Angular 4 应用程序和 ASP.NET Core 2.0 后端。我有 Angular 应用程序(使用 Angular-cli 生成)和 .net core Web api(使用 vs 2017 模板生成)。 在 Angular 一侧,我使用 angular-oauth2-oidc 。我使用 AzureAD app registration portal 注册了我的应用程序(应用程序注册为v2.0)在应用程序配置中有两个平台Web和Web API。 在 Web api 平台中,定义了名为“api:///access_as_user”的范围,并且我的应用程序有权访问该范围。 App registration

在 Angular 一侧就是这样。在.NET方面有.AddJwtBearer()方法,它配置了权限、受众(clientId)。

services.AddAuthentication(auth =>
        {
            auth.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            auth.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddCookie()
        .AddJwtBearer(cfg =>
        {
            cfg.Authority = "https://login.microsoftonline.com/<tenantId>/v2.0";                
            cfg.Audience = "<clientId>";
            //cfg.Configuration = new Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfiguration();
            cfg.TokenValidationParameters = new TokenValidationParameters()
            {
                ValidateAudience = false,
                ValidIssuer = "https://login.microsoftonline.com/<tenantId>/v2.0"
            };              
        });

当我尝试从客户端应用程序访问我的 Web api 时,会出现此问题。如果我不要求我的范围(“api:///access_as_user”)在 Angular ,Web api返回401未经授权。我要求我得到它

"AADSTS65005:The application 'Angular-test' asked for scope 'access_as_user' that doesn't exist on the resource. Contact the app vendor.
Trace+ID: c55338dd-35c8-429b-bfe1-5c48ac030d00
Correlation+ID: a0b4bc2d-7f15-4ca4-9cd5-4fe61999e4d9 
Timestamp:+2017-10-24+10:35:56Z""

有人有相同/类似的问题吗?

Git 存储库:

Client --> 分支oidc

Servier

最佳答案

我可以不使用自定义范围来调用 Web api。以下是您引用的步骤:

1.使用隐式流程获取 token ,如下所示:

GET: https://login.microsoftonline.com/common/oauth2/v2.0/authorize?response_type=id_token&client_id=1e6af2ed-686c-4914-96ed-0cd7b1673cbb&scope=openid&redirect_uri=http%3A%2F%2Flocalhost&nonce=123

2.1 Multi-Tenancy :

使用上面的 id_token 调用 .net core Web API,以保护 Azure AD V2.0 应用程序,如下面的代码:

app.UseJwtBearerAuthentication(new JwtBearerOptions
{
        Authority = "https://login.microsoftonline.com/common/v2.0/",
        Audience = Configuration["Authentication:AzureAd:ClientId"],
        Events = new JwtBearerEvents
        {
            OnAuthenticationFailed = AuthenticationFailed
        },
        TokenValidationParameters=new Microsoft.IdentityModel.Tokens.TokenValidationParameters
        {
            ValidateIssuer =false,          
        } 
    });        
});

2.1 根据需要限制租户:

app.UseJwtBearerAuthentication(new JwtBearerOptions
{
    Authority = "https://login.microsoftonline.com/common/v2.0/",
    Audience = Configuration["Authentication:AzureAd:ClientId"],
    Events = new JwtBearerEvents
    {
        OnAuthenticationFailed = AuthenticationFailed
    },
    TokenValidationParameters=new Microsoft.IdentityModel.Tokens.TokenValidationParameters
    {
        ValidateIssuer =true,
        ValidIssuers=new string[] { "list the allowed issues here","https://login.microsoftonline.com/xxxxxxxx-0e9b-42f8-8b22-3c4a2f1d8800/v2.0"}
    } 
});

您可以引用下面有关使用 Azure AD V2.0 应用程序保护 Web api 的代码示例。该代码示例适用于Azure AD B2C,我们可以修改其权限以使其适用于Azure AD V2.0应用程序。如果您仍然遇到问题,请随时告诉我。

active-directory-b2c-dotnetcore-webapi

关于angular - Azure AD 2.0 应用程序 - 无法从 Angular 客户端访问范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46910026/

相关文章:

c# - 如何在 Azure AD 身份验证中将多个资源刷新 token 更改为 true?

azure - 如何以编程方式向外部用户授予对 azure 资源组的访问权限

javascript - 错误类型错误 : Cannot read property 'salt' of null

angular - Angular 4 中的 Websocket 和 RxJS 混淆

arrays - 在 typescript 中遍历数组

c# - .NET Core Web应用程序启动类中的app.UseMigrationsEndPoint做什么

angular 5删除组件销毁上的样式节点

entity-framework - 在 ASP.NET Core Windows 服务项目中解析 EFv6.3 中的连接字符串

.NET CORE 身份主键

c# - 无法访问我的 Azure Web 应用程序的 "Authentication/Authorization"