![](/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.