C# MVC获取Azure Active Directory认证用户的givenname和姓氏

C# MVC获取Azure Active Directory认证用户的givenname和姓氏,c#,asp.net-mvc,azure,azure-active-directory,C#,Asp.net Mvc,Azure,Azure Active Directory,我可以通过以下方式获取经过身份验证的用户的用户名(电子邮件格式): var autenticateduser=HttpContext.User.Identity.Name 使用QuickWatch窗口,我可以使用以下表达式查找givenname和姓氏。有没有更干净的方法来获取这些信息 (new System.Linq.SystemCore\u EnumerableDebugView(((System.Security.Claims.ClaimsIdentity)(HttpContext.User

我可以通过以下方式获取经过身份验证的用户的用户名(电子邮件格式):

var autenticateduser=HttpContext.User.Identity.Name

使用QuickWatch窗口,我可以使用以下表达式查找givenname和姓氏。有没有更干净的方法来获取这些信息

(new System.Linq.SystemCore\u EnumerableDebugView(((System.Security.Claims.ClaimsIdentity)(HttpContext.User.Identity)).Claims)).Items[5]

怎么样

ClaimsPrincipal cp = ClaimsPrincipal.Current;
string welcome = string.Format("Welcome, {0} {1}!", cp.FindFirst(ClaimTypes.GivenName).Value, cp.FindFirst(ClaimTypes.Surname).Value);

通过这种方式,您还可以获得非标准声明,您只需要知道密钥的名称,例如可以从openidconnect YOURDOMAIN/.well-known/openid配置JSON响应中获得

你好@User.Claims.Single(a=>a.Type==“”)。值