Azure AD B2C 令牌返回名称,但 User.Identity.Name 为空 [英] Azure AD B2C Token returns name but User.Identity.Name is null
本文介绍了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
.为什么它是空的?不应该等于截图中的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 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文