我发现此链接:
但是答案都是过时的!
因此,这篇SO帖子既不能作为解决方案,也可以作为一种解决方法:我应该如何从MVC 6视图中访问ApplicationUser属性?
在mvc动作中:base.User不是ApplicationUser类型,也不是base.User.Identity。两者都不能转换为ApplicationUser。
因此,登录后如何访问放置在applicationuser对象中的自定义属性?
对于控制器,请依赖UserManager<ApplicationUser>
。然后,您可以通过请求的HttpContext访问ApplicationUser。我为我的项目写了一个扩展方法:
public static class IdentityExt
{
public static Task<T> GetCurrentUser<T>(this UserManager<T> manager, HttpContext httpContext) where T: class{
return manager.GetUserAsync(httpContext.User);
}
}
这将返回当前用户,并允许您访问其所有属性。
这是一个动作内部:
public Task<IActionResult> Index(){
var user = await _userManager.GetCurrentUser(HttpContext);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句