【问题标题】:Azure AD B2C Token returns name but User.Identity.Name is nullAzure AD B2C 令牌返回名称,但 User.Identity.Name 为空
【发布时间】:2018-09-03 05:42:40
【问题描述】:

我有一个 Azure AD B2C 令牌,它似乎正确地返回了当前登录的用户名。这是我登录后用于解码应用程序返回的令牌的 jwt.ms 的屏幕截图:

但是,然后我尝试在我的_Layout.cshtml 中使用@User.Identity.Name。为什么它是空的?不应该等于截图中的“name”值吗?

【问题讨论】:

  • 您可能需要告诉中间件在哪里可以找到Name,但这取决于您使用的中间件。
  • 我正在使用以下内容:app.UseKentorOwinCookieSaver();。那就是你所说的“中间件”?
  • 好奇...你为什么用Kentor.OwinCookieSaver
  • @spottedmahn 我正在使用它“以防万一”这是问题所在:stackoverflow.com/questions/31720820/…。我可能可以删除它。

标签: oauth-2.0 jwt azure-ad-b2c


【解决方案1】:

请参阅使用 Owin 的 this working example(听起来您正在使用)。

<ul class="nav navbar-nav navbar-right">
    <li>
        <a id="profile-link">@User.Identity.Name</a>
        ...
    </li>
</ul>

Source

public void ConfigureAuth(IAppBuilder app)
{
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

    app.UseCookieAuthentication(new CookieAuthenticationOptions());

    app.UseOpenIdConnectAuthentication(

        ...
    );
}

Source

【讨论】:

    【解决方案2】:

    原来我错过了由 cmets 标记的行:

            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}"
                }
            );
    

    整个文件位于此处:https://github.com/Azure-Samples/active-directory-b2c-dotnet-webapp-and-webapi/blob/master/TaskWebApp/App_Start/Startup.Auth.cs

    【讨论】:

    猜你喜欢
    • 2018-01-21
    • 1970-01-01
    • 2021-03-03
    • 2021-10-26
    • 2020-09-09
    • 2017-11-23
    • 2018-01-08
    • 2020-05-10
    • 1970-01-01
    相关资源
    最近更新 更多