从asp.net核心标识获取applicationuser的属性 [英] Get property of applicationuser from the asp.net core identity
问题描述
我找到了此链接:
但是答案都过时了!
And this SO post can not neither be a solution, its just a boilerplate workaround: How should I access my ApplicationUser properties from within my MVC 6 Views?
在mvc动作中:base.User不是ApplicationUser类型,也不是base.User.Identity.两者都不能转换为ApplicationUser.
In the mvc action: base.User is not of type ApplicationUser neither is base.User.Identity. Both can not be casted into ApplicationUser.
那么登录后如何访问放置在applicationuser对象中的自定义属性?
So how can I access my custom properties put inside the applicationuser object when logged in?
推荐答案
对于控制器,请依赖UserManager<ApplicationUser>
.然后,您可以通过请求的HttpContext访问ApplicationUser.我为我的项目写了一个扩展方法:
For the controller, have a dependency on UserManager<ApplicationUser>
. Then, you can access the ApplicationUser through the HttpContext of the request. I wrote an extension method for my project:
public static class IdentityExt
{
public static Task<T> GetCurrentUser<T>(this UserManager<T> manager, HttpContext httpContext) where T: class{
return manager.GetUserAsync(httpContext.User);
}
}
这将返回当前用户,并允许您访问其所有属性.
That returns the current user, and will allow you to access all of their properties.
这里是动作内:
public Task<IActionResult> Index(){
var user = await _userManager.GetCurrentUser(HttpContext);
}
这篇关于从asp.net核心标识获取applicationuser的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!