在身份中获取当前用户的电子邮件
get current user email in Identity
我使用 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 容器中添加此代码:_.For<IIdentity>().Use(() => GetIdentity());
[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();
可以在ASP.NET Identity
获取当前用户如下图:
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext()
.GetUserManager<ApplicationUserManager>()
.FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
//If you use int instead of string for primary key, use this:
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext()
.GetUserManager<ApplicationUserManager>()
.FindById(Convert.ToInt32(System.Web.HttpContext.Current.User.Identity.GetUserId()));
从 AspNetUsers
table:
获取自定义属性
ApplicationUser user = UserManager.FindByName(userName);
string mail= user.Email;
希望这对您有所帮助...
家庭控制器
[AllowAnonymous]
public ActionResult GetUserEmail()
{
try
{
var userID = User.Identity.GetUserId();
ViewBag.EmailData = db.AspNetUsers.Where(s => s.Id == userID).Select(x => x.Email).FirstOrDefault().ToString();
return Content(ViewBag.EmailData);
}
catch
{
return null;
}
}
并在视图中
@{
string USREmail = Html.Action("GetUserEmail", "Home").ToString();
}
您可以显示电子邮件
@USREmail
我使用 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 容器中添加此代码:_.For<IIdentity>().Use(() => GetIdentity());
[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();
可以在ASP.NET Identity
获取当前用户如下图:
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext()
.GetUserManager<ApplicationUserManager>()
.FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
//If you use int instead of string for primary key, use this:
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext()
.GetUserManager<ApplicationUserManager>()
.FindById(Convert.ToInt32(System.Web.HttpContext.Current.User.Identity.GetUserId()));
从 AspNetUsers
table:
ApplicationUser user = UserManager.FindByName(userName);
string mail= user.Email;
希望这对您有所帮助...
家庭控制器
[AllowAnonymous]
public ActionResult GetUserEmail()
{
try
{
var userID = User.Identity.GetUserId();
ViewBag.EmailData = db.AspNetUsers.Where(s => s.Id == userID).Select(x => x.Email).FirstOrDefault().ToString();
return Content(ViewBag.EmailData);
}
catch
{
return null;
}
}
并在视图中
@{
string USREmail = Html.Action("GetUserEmail", "Home").ToString();
}
您可以显示电子邮件
@USREmail