簡體   English   中英

如何驗證 Azure AD v2 在 ASP.NET 核心 WEB ZDB97Z42387108CA81463 中生成的 OpenID Connect 訪問令牌

[英]How to Validate OpenID Connect Access Token generated by Azure AD v2 in ASP.NET core WEB API?

如何驗證 ASP.NET 核心 WEB API 中的 Azure AD (v2?!!) 生成的 OpenID Connect 訪問令牌?

情景是:

我有一個 Angular 8 客戶端應用程序,它在登錄后獲取 OpenID Connect 訪問令牌。 客戶端可以連同訪問令牌一起調用 API。 但問題是,我應該如何驗證 ASP.NET 核心 API 中的令牌?

使用此代碼,我得到一個沒有任何描述的授權錯誤。

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

        services.AddCors(options =>
        {
            options.AddDefaultPolicy(
                builder =>
                {
                    builder.AllowAnyOrigin();
                    builder.AllowAnyMethod();
                    builder.AllowAnyOrigin();
                    builder.AllowAnyHeader();
                });
        });

Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:信息:授權失敗。 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:信息:過濾器“Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter”處的請求授權失敗。 Microsoft.AspNetCore.Mvc.ChallengeResult:信息:使用身份驗證方案執行 ChallengeResult ()。 Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler:信息:AuthenticationScheme:AzureADJwtBearer 受到挑戰。

"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "localhost",
"TenantId": "myTenantId",
"ClientId": "myClientId"

},

Domain不是localhost 僅當您想要接受來自單個租戶的訪問令牌時才需要指定 TenantId。 否則,您可以將它們設置為common

This value can be:
- A GUID (Tenant ID = Directory ID)
- 'common' (any organization and personal accounts)
- 'organizations' (any organization)
- 'consumers' (Microsoft personal accounts)

您應該通過將此代碼添加到Startup.cs文件來更改為使用 Microsoft 標識平台端點(Azure AD V2.0 端點):

services.Configure<JwtBearerOptions>(AzureADDefaults.JwtBearerAuthenticationScheme, options =>
{
    // This is a Microsoft identity platform web API.
    options.Authority += "/v2.0";

    .....
});

您可以單擊此處查看詳細說明和代碼示例。

暫無
暫無

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

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