簡體   English   中英

Azure AD 2.0 應用程序 - 無法從 Angular 客戶端訪問范圍

[英]Azure AD 2.0 application - can't access scope from angular client

我正在嘗試開發 Angular 4 應用程序和 ASP.NET Core 2.0 后端。 我有 angular 應用程序(使用 Angular-cli 生成)和 .net core web api(使用 vs 2017 模板生成)。 在角度方面,我使用angular-oauth2-oidc 我使用 AzureAD應用程序注冊門戶(應用程序注冊為 v2.0)在應用程序配置中有兩個平台 Web 和 Web API 注冊了我的應用程序。 在 Web api 平台中,定義了名為“api:///access_as_user”的范圍,我的應用程序可以訪問此范圍。 應用注冊

在角度方面就是這樣。 在 .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 時出現問題。 如果我不在 angular 中要求我的范圍(“api:///access_as_user”),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 存儲庫:

客戶端--> 分支oidc

服務商

我可以通過不使用自定義范圍來調用 web api。 以下步驟供您參考:

1.使用如下所示的隱式流程獲取令牌:

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多租戶

使用上面的 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 應用程序。 如果您仍有問題,請隨時告訴我。

活動目錄-b2c-dotnetcore-webapi

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM