Get user logged in to Asp.Net Core 2.0

Asked

Viewed 1,437 times

3

How do I get the User who is logged in to 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;
   }
}

However, it is not working in Asp.Net Core 2.0:

inserir a descrição da imagem aqui

  • 1

    You should take these values from within your controllers.

  • worked well, I took the values in the controller and sent to the object I was going to use, vlw

  • Friend I will put as an answer, if I can score as valid answer thank you.

4 answers

1

You can either get value in class or control.

The response of @Maycon F. Castro, is wrong, you can yes, get user name or identification 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();
}

Sources : Content, Claimsprincipal.

I hope I’ve helped.

1

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

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;
   }
}
  • 2

    Very interesting, +1;

0

Complementing(Another option)...

another option to catch 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();
}

0

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

string currentUser = User.Identity.Name;

It is also possible to check if the user is in particular Role:

if(User.IsInRole("nomeDoSeuRole"))
{
    // Seu código
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.