Get property of applicationuser from the asp.net core identity

不羁岁月 提交于 2019-12-01 08:03:44

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.

Here it is inside an action:

public Task<IActionResult> Index(){
     var user = await _userManager.GetCurrentUser(HttpContext);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!