oauth-2.0 - Azure AD B2C : User. Identity.Name 为 null,但 User.Identity.m_instance_claims[9] 具有名称

标签 oauth-2.0 azure-active-directory azure-ad-b2c

用户通过我的 Azure AD B2C Web 应用程序身份验证后,我尝试检索 User.Identity.Name;然而,它是空的。然而,User.Identity.m_instance_claims[9](如下面的屏幕截图所示)确实具有正确的名称。

怎么会这样?如何获取 User.Identity.Name = User.Identity.m_instance_claims[9]

(请注意,后者是私有(private)变量,不能用作 User.Identity.Name 的替代品。 enter image description here


更新

我还在 Web.config 文件中添加了以下内容:

<configuration>  
  <configSections>  
    <!--WIF 4.5 sections -->  
    <section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>  
    <section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>  
  </configSections>  

...

  <system.identityModel>
    <identityConfiguration>
      <securityTokenHandlers>
        <add type="System.IdentityModel.Tokens.SamlSecurityTokenHandler, System.IdentityModel">
          <samlSecurityTokenRequirement>
            <nameClaimType value="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name" />
          </samlSecurityTokenRequirement>
        </add>
      </securityTokenHandlers>
    </identityConfiguration>
  </system.identityModel>
</configuration>

不幸的是,这仍然给出User.Identity.Name = null

最佳答案

我相信您需要在 web.config 中设置正确的 nameClaimType:

https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/windows-identity-foundation/nameclaimtype


更新

除上述内容外,还缺少以下代码:

// Specify the claims to validate
TokenValidationParameters = new TokenValidationParameters
{
    NameClaimType = "name"
},

参见this link了解如何使用上述内容。

关于oauth-2.0 - Azure AD B2C : User. Identity.Name 为 null,但 User.Identity.m_instance_claims[9] 具有名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49469979/

相关文章:

python-3.x - 将 Azure AD 中的主体 ID 解析为用户、服务

asp.net-core-2.0 - Azure B2C 成功登录后动态重定向

facebook - 如何从 Facebook 签名请求中获取访问 token ?

java - ADAL 4 Android 不传递客户端 secret

node.js - 使用我的自己的Web应用程序使用passwordjs管理单点登录-共享登录

spring-boot - 我如何使用 mock 测试 OAuth2 资源服务器

azure - 使用 azure 进行身份验证时 Post_Logout_Redirect_Uri 不会重定向

azure - 如何从应用程序(客户端)ID 和目录(租户)ID 获取对象 ID?

azure - 将 Azure AD B2C 与 Xamarin 应用程序集成

c# - Azure B2C 身份提供商登录失败 : AADB2C99002: User does not exist