加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-identity – IIdentity.Name与IIdentity.GetUserName()

发布时间:2020-12-16 04:38:29 所属栏目:asp.Net 来源:网络整理
导读:扩展方法位于Microsoft.AspNet.Identity中.那有什么区别?这些2什么时候会返回不同的值? var idName = User.Identity.Name;var idGetName = User.Identity.GetUserName(); 解决方法 扩展方法的实现是这样的; public static string GetUserName(this IIdenti
扩展方法位于Microsoft.AspNet.Identity中.那有什么区别?这些2什么时候会返回不同的值?
var idName = User.Identity.Name;
var idGetName = User.Identity.GetUserName();

解决方法

扩展方法的实现是这样的;
public static string GetUserName(this IIdentity identity)
{
    if (identity == null)
    {
        throw new ArgumentNullException("identity");
    }
    ClaimsIdentity claimsIdentity = identity as ClaimsIdentity;
    if (claimsIdentity == null)
    {
        return null;
    }
    return claimsIdentity.FindFirstValue("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name");
}

IIdentity.Name和IdentityExtensions.GetUserName()之间唯一明显的返回值差异是,如果底层IIdentity实现不是ClaimsIdentity,则GetUserName()始终返回null,而Name属性将返回底层IIdentity实现返回的任何内容.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读