asp.net-mvc - 使用 Azure Active Directory OAuth 时没有名字或姓氏声明

标签 asp.net-mvc azure oauth-2.0 azure-active-directory

我们正在使用 Azure Active Directory 验证我们的 MVC 应用程序,但我们在 ClaimsPrincipal 中获得的唯一信息是名称和组成员身份。

我们还需要访问用户的名字和姓氏。有关我们如何解决此问题的任何指示吗?

最佳答案

OpenID Connect 引入了 id_token(这是 JWT)。

查看documentation , id_token 包含一些可能匹配的声明:

  • given_name:提供用户的名字或“给定”名称,如在 Azure AD 用户对象上设置的那样。
  • family_name:提供 Azure AD 用户对象中定义的用户的姓氏或姓氏。
  • unique_name:提供人类可读的值,用于标识 token 的主题。该值不保证在租户内是唯一的,并且设计为仅用于显示目的。

因此,在您的 Controller 中,您可以像这样访问这些声明:

using System.Security.Claims;
...
var identity = (ClaimsIdentity)User.Identity;
var lastName = identity.Claims.First(
    c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname").Value;
var firstName = identity.Claims.First(
    c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname").Value;
var displayName = identity.Claims.First(c => c.Type == "name").Value;

这些声明对应于您的 Azure AD 中的名字、姓氏和显示名称:

Azure AD Basic User Information

关于asp.net-mvc - 使用 Azure Active Directory OAuth 时没有名字或姓氏声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39089608/

相关文章:

azure - 使用 Azure Key Vault 时如何从客户端隐藏 API key ?

javascript - 如何使用环回设置 OAuth 2.0 服务器

security - 自动关联社交登录有什么安全风险(Oauth2)

c# - 如何从旧的 asp.net 成员资格创建用户到新的 MVC .net 并发送密码重置

Azure VM 上的 Azure Pipelines 代理

php - Azure 表存储 Rest API

spring - 如何禁用 JWT token

asp.net - app_offline.htm 在生产设备上抛出 HTTP 500 错误

asp.net-mvc - ASP .Net MVC 中多个设备的多 View

Javascript 代码组织数据驱动的应用程序