【问题标题】:MVC Get givenname and surname of Azure Active Directory authenticated userMVC 获取 Azure Active Directory 身份验证用户的名字和姓氏
【发布时间】:2018-06-19 05:44:50
【问题描述】:

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

var autenticateduser = HttpContext.User.Identity.Name;

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

(new System.Linq.SystemCore_EnumerableDebugView<System.Security.Claims.Claim>(((System.Security.Claims.ClaimsIdentity)(HttpContext.User.Identity)).Claims)).Items[5]

【问题讨论】:

    标签: c# asp.net-mvc azure azure-active-directory


    【解决方案1】:

    怎么样

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

    【讨论】:

      【解决方案2】:

      这样您也可以获得非标准声明,您只需要知道密钥的名称,例如可以从 openidconnect YOURDOMAIN/.well-known/openid-configuration JSON 响应中获得。

      你好@User.Claims.Single(a => a.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname").Value!

      system.security.claims.claimsprincipal.claims API reference

      【讨论】:

        猜你喜欢
        • 2017-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-29
        相关资源
        最近更新 更多