我使用 IIdentity
接口来获取当前的 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;
}
在我的IoC容器
[structuremap]
中,添加以下代码:_.For<IIdentity>().Use(() => GetIdentity());
使用方法
this._identity.GetUserId();
this._identity.GetUserName();
this._identity.IsAuthenticated
现在我想要实现GetEmailAdress
方法,怎么做呢?
示例
this._identity.GetEmailAdress();
当使用 this._identity.GetUserName();
时,不要从数据库中获取用户名。