ASP.NET Core Identity - get current user

前端 未结 7 676
灰色年华
灰色年华 2020-12-07 11:48

To get the currently logged in user in MVC5, all we had to do was:

using Microsoft.AspNet.Identity;
[Authorize]
public IHttpActionResult DoSomething() {
             


        
7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 12:39

    I have put something like this in my Controller class and it worked:

    IdentityUser user = await userManager.FindByNameAsync(HttpContext.User.Identity.Name);
    

    where userManager is an instance of Microsoft.AspNetCore.Identity.UserManager class (with all weird setup that goes with it).

提交回复
热议问题