扩展方法在Microsoft.AspNet.Identity
中。那有什么区别呢?这两个何时会返回不同的值?
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实现不是GetUserName()
,则ClaimsIdentity
始终返回null,而Name
属性将返回基础IIdentity实现返回的内容。关于asp.net-identity - IIdentity.Name与IIdentity.GetUserName()扩展方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20830855/