![](/img/trans.png)
[英]How to get Identity Server 4's access_token in ASP Net 4.7 MVC Application
[英]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.