扩展方法在Microsoft.AspNet.Identity中。那有什么区别?这两个值何时会返回不同的值?
var idName = User.Identity.Name;
var idGetName = User.Identity.GetUserName();发布于 2013-12-30 00:04:46
可拓方法的实现类似于;
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实现返回的任何内容。
https://stackoverflow.com/questions/20830855
复制相似问题