我们正在使用 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 中的名字、姓氏和显示名称:
关于asp.net-mvc - 使用 Azure Active Directory OAuth 时没有名字或姓氏声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39089608/