本文介绍了Azure AD B2C令牌返回名称,但User.Identity.Name为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Azure AD B2C令牌,它似乎正确返回了当前登录用户的名称.这是jwt.ms的屏幕截图,登录后我将用它来解码应用程序返回的令牌:
I have an Azure AD B2C token that seems to be correctly returning the currently logged-in user's name. Here is a screenshot from jwt.ms which I am using to decode the token returned by the application after I have logged in:
但是,然后我尝试在_Layout.cshtml
中使用@User.Identity.Name
.为什么它为空?它不应该等于屏幕截图中的名称"值吗?
However, then I attempt to use @User.Identity.Name
in my _Layout.cshtml
. Why is it null? Shouldn't it be equal to the "name" value in the screenshot?
推荐答案
事实证明我缺少注释标记的行:
It turned out I was missing the line marked by the comments:
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
// Generate the metadata address using the tenant and policy information
MetadataAddress = String.Format(AadInstance, Tenant, DefaultPolicy),
// These are standard OpenID Connect parameters, with values pulled from web.config
ClientId = ClientId,
Authority = Authority,
PostLogoutRedirectUri = RedirectUri,
RedirectUri = RedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications()
{
RedirectToIdentityProvider = OnRedirectToIdentityProvider,
AuthenticationFailed = OnAuthenticationFailed,
AuthorizationCodeReceived = OnAuthorizationCodeReceived,
},
//////// WAS MISSING THIS BELOW /////////
// Specify the claims to validate
TokenValidationParameters = new TokenValidationParameters
{
// This claim is in the Azure AD B2C token; this code tells the web app to "absorb" the token "name" and place it in the user object
NameClaimType = "name"
},
// Specify the scope by appending all of the scopes requested into one string (separated by a blank space)
Scope = $"{OpenIdConnectScopes.OpenId} {ReadTasksScope} {WriteTasksScope}"
}
);
这篇关于Azure AD B2C令牌返回名称,但User.Identity.Name为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!