ASP.NET Core Identity - get current user

前端 未结 7 639
灰色年华
灰色年华 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:52

    If you are using Bearing Token Auth, the above samples do not return an Application User.

    Instead, use this:

    ClaimsPrincipal currentUser = this.User;
    var currentUserName = currentUser.FindFirst(ClaimTypes.NameIdentifier).Value;
    ApplicationUser user = await _userManager.FindByNameAsync(currentUserName);
    

    This works in apsnetcore 2.0. Have not tried in earlier versions.

    0 讨论(0)
提交回复
热议问题