簡體   English   中英

為什么添加 Microsoft Identity auth 會導致另一個策略的 GetTokenAsync("access_token") 返回 null?

[英]Why does adding Microsoft Identity auth cause another policy's GetTokenAsync("access_token") to return null?

問題:

我有一個帶有 OpenIdConnect 身份驗證的 ASP.NET Core 3.1 Web 應用程序。 我在單獨的(非默認)策略中添加了 Microsoft 身份驗證 (Azure Active Directory v2),以便在一個特定控制器上使用。

使用以下行(在 Startup.cs 中)添加 Microsoft Identity 會導致httpContext.GetTokenAsync("access_token")在由使用我的原始 OpenIdConnect 實現授權的控制器調用時返回 null。 被調用的httpContext仍然具有來自原始 OpenIdConnect 實現的所有自定義聲明。 刪除以下行解決了問題,我可以成功獲取訪問令牌。

services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAd", "AzureAdOpenIdConnect", "AzureAdOpenIdCookies");

其他詳情:

Microsoft Identity 實施是在單獨的策略上

services.AddAuthorization(options =>
{
    options.DefaultPolicy = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .AddAuthenticationSchemes("oidcFlow") // scheme name for my original OpenIdConnect auth implementation
        .Build();

    options.AddPolicy("AzureAdOidc", new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .AddAuthenticationSchemes("AzureAdOpenIdConnect", "AzureAdOpenIdCookies") // scheme names used above when initializing Microsoft Identity
        .Build());
});

沒有控制器帶有AzureAdOidc策略被嘗試獲取訪問令牌的默認策略控制器調用。

更新:

AddMicrosoftIdentityWebAppAuthentication調用移到 ConfigureServices(在 Startup.cs 中)中的其他身份驗證實現之上解決了這個問題,我可以再次從使用我的其他身份驗證實現進行身份驗證的調用中獲取訪問令牌。

暫無
暫無

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

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