Get logged in to Asp.Net Core 2.0

3

How do I get the User that is logged into the class?

I'm trying this way:

public class Teste
{        
   private readonly UserManager<Usuario> _userManager;

   public Teste(UserManager<Usuario> userManager)
   {
      _userManager = userManager;
   }
   public async Task<Usuario> GetUser()
   {
      var user = await _userManager.GetUserAsync(HttpContext.User);
      return user;
   }
}

But it is not working in Asp.Net Core 2.0:

    
asked by anonymous 07.06.2018 / 20:11

4 answers

1

Through dependency injection, you can have access to your HttpContext in any part of the application that receives objects by injection. Just inject the IHttpContextAccessor interface.

public class Teste
{        
   private readonly UserManager<Usuario> _userManager;
   private readonly IHttpContextAccessor _httpContextAccessor

   public Teste(UserManager<Usuario> userManager, IHttpContextAccessor httpContextAccessor)
   {
      _userManager = userManager;
      _httpContextAccessor = httpContextAccessor;
   }
   public async Task<Usuario> GetUser()
   {
      var user = await _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User);
      return user;
   }
}
    
12.06.2018 / 21:53
0

You can either get value in the class or the control.

The response from @Maycon F. Castro , is wrong, you can get the username or ID using ClaimsPrincipal , see the example below:

Class:

public static class MinhaClasse
{
    public static string GetUserId(this ClaimsPrincipal claimsPrincipal)
    {
        if (claimsPrincipal == null)
        {
            throw new ArgumentNullException(nameof(claimsPrincipal));
        }
        return claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
    }

    public static string GetUserName(this ClaimsPrincipal claimsPrincipal)
    {
        if (claimsPrincipal == null)
        {
            throw new ArgumentNullException(nameof(claimsPrincipal));
        }
        return claimsPrincipal.FindFirst(ClaimTypes.Name)?.Value;
    }
}

HomeController:

public IActionResult Index()
{    
    var id = Content(User.GetUserId()).Content; //"dd76f866-a04b-4517-ba1f-7bf35a1ae2c8"
    var name = Content(User.GetUserName()).Content; //[email protected]

    return View();
}

Fonts : Content , ClaimsPrincipal .

I hope I have helped.

    
12.06.2018 / 21:04
0

Complementing (Another option) ...

Another option to get the logged in user is to do this directly by controller

private readonly UserManager<Usuario> _userManager;
public SeuController(UserManager<Usuario> userManager)
{
    _userManager = userManager;
}

public async Task<IActionResult> Index()
{
    Usuariouser = await _userManager.GetUserAsync(User);
    return View();
}
    
12.06.2018 / 21:40
0

Just complementing with an option that can only be used within Controller .

string currentUser = User.Identity.Name;

You can also check if the user is in certain Role :

if(User.IsInRole("nomeDoSeuRole"))
{
    // Seu código
}
    
12.06.2018 / 22:28