我使用 IIdentity
获取电流的接口(interface)userid
和 username
.
所以实现下面的方法:
private static IIdentity GetIdentity()
{
if (HttpContext.Current != null && HttpContext.Current.User != null)
{
return HttpContext.Current.User.Identity;
}
return ClaimsPrincipal.Current != null ? ClaimsPrincipal.Current.Identity : null;
}
并添加此代码:_.For<IIdentity>().Use(() => GetIdentity());
在我的 IoC 容器中 [structuremap]
.
用法
this._identity.GetUserId();
this._identity.GetUserName();
this._identity.IsAuthenticated
现在我要实现GetEmailAdress
方法,如何做到这一点?
示例
this._identity.GetEmailAdress();
当使用 this._identity.GetUserName();
不要从数据库获取用户名。
最佳答案
你可以在这些行上做一些事情:
public static class IdentityExtensions
{
public static string GetEmailAdress(this IIdentity identity)
{
var userId = identity.GetUserId();
using (var context = new DbContext())
{
var user = context.Users.FirstOrDefault(u => u.Id == userId);
return user.Email;
}
}
}
然后你就可以访问它了:
this._identity.GetEmailAdress();
关于c# - 在身份中获取当前用户的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42532765/