获取身份中的当前用户电子邮件 [英] get current user email in Identity
本文介绍了获取身份中的当前用户电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用IIdentity
界面获取当前的userid
和username
.
I use IIdentity
interface For Getting current userid
and 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;
}
然后在我的IoC容器[structuremap]
中添加此代码:_.For<IIdentity>().Use(() => GetIdentity());
.
And Add this code: _.For<IIdentity>().Use(() => GetIdentity());
in my IoC Container[structuremap]
.
用法
this._identity.GetUserId();
this._identity.GetUserName();
this._identity.IsAuthenticated
现在我要实现GetEmailAdress
方法,该怎么做?
Now I Want to Implement GetEmailAdress
Method, How To do this?
示例
this._identity.GetEmailAdress();
使用this._identity.GetUserName();
时,请勿获取用户名数据库.
When use this._identity.GetUserName();
do not get username form database.
推荐答案
您可以在以下几行中做一些事情:
You could do something on these lines:
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;
}
}
}
然后您将可以像以下方式访问它:
and then you will be able to access it like:
this._identity.GetEmailAdress();
这篇关于获取身份中的当前用户电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文