Security.Principal.IIdentity 获取用户邮箱的扩展方法
Security.Principal.IIdentity extension method for getting user email
在我的项目中 ASP 我正在使用 ASP.NET Identity 2.2.1。在许多地方,我必须获得当前(登录)用户的电子邮件。
现在我发现那个用户使用这个:
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId<int>());
var email = user.Email;
我注意到 GetUserId<T>
是一个扩展方法,可以在 IdentityExtensions
class 和 Microsoft.AspNet.Identity
中找到
我创建了自己的扩展方法,通过允许以以下方式获取电子邮件来简化获取电子邮件的过程:
var email = User.Identity.GetUserEmail()
下面是我的分机:
public static class MyIIdentityExtensions
{
public static string GetUserEmail(this IIdentity identity)
{
if (identity == null)
{
throw new ArgumentNullException("identity");
}
var ci = identity as ClaimsIdentity;
if (ci == null) return null;
var um = HttpContext.Current.GetOwinContext().GetUserManager<UserManager>();
if (um == null) return null;
var user = um.FindById(ci.GetUserId<int>());
if (user == null) return null;
return user.Email;
}
}
复杂多了
我可以简化一下吗?也许有这样做的内置方法?我想要的是从 User.Identity.
获取当前登录用户的 Email
的简单方法
如果使用 UserManager
,每次调用 GetUserEmail
方法时都会访问数据库。
相反,您可以将电子邮件添加为声明。在 ApplicationUser
class 里面有 GenerateUserIdentityAsync
方法
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
userIdentity.AddClaim(new Claim(ClaimTypes.Email, this.Email));
return userIdentity;
}
然后你的扩展方法去搞定
public static class IdentityExtensions
{
public static string GetUserEmail(this IIdentity identity)
{
if (identity == null)
{
throw new ArgumentNullException("identity");
}
var ci = identity as ClaimsIdentity;
if (ci != null)
{
return ci.FindFirstValue(ClaimTypes.Email);
}
return null;
}
}
在我的项目中 ASP 我正在使用 ASP.NET Identity 2.2.1。在许多地方,我必须获得当前(登录)用户的电子邮件。 现在我发现那个用户使用这个:
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId<int>());
var email = user.Email;
我注意到 GetUserId<T>
是一个扩展方法,可以在 IdentityExtensions
class 和 Microsoft.AspNet.Identity
我创建了自己的扩展方法,通过允许以以下方式获取电子邮件来简化获取电子邮件的过程:
var email = User.Identity.GetUserEmail()
下面是我的分机:
public static class MyIIdentityExtensions
{
public static string GetUserEmail(this IIdentity identity)
{
if (identity == null)
{
throw new ArgumentNullException("identity");
}
var ci = identity as ClaimsIdentity;
if (ci == null) return null;
var um = HttpContext.Current.GetOwinContext().GetUserManager<UserManager>();
if (um == null) return null;
var user = um.FindById(ci.GetUserId<int>());
if (user == null) return null;
return user.Email;
}
}
复杂多了
我可以简化一下吗?也许有这样做的内置方法?我想要的是从 User.Identity.
获取当前登录用户的Email
的简单方法
如果使用 UserManager
,每次调用 GetUserEmail
方法时都会访问数据库。
相反,您可以将电子邮件添加为声明。在 ApplicationUser
class 里面有 GenerateUserIdentityAsync
方法
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
userIdentity.AddClaim(new Claim(ClaimTypes.Email, this.Email));
return userIdentity;
}
然后你的扩展方法去搞定
public static class IdentityExtensions
{
public static string GetUserEmail(this IIdentity identity)
{
if (identity == null)
{
throw new ArgumentNullException("identity");
}
var ci = identity as ClaimsIdentity;
if (ci != null)
{
return ci.FindFirstValue(ClaimTypes.Email);
}
return null;
}
}