5

I have created a angular 2 application using .NET Core and MVC. I want to know the login id of user. How to get logged in id of a user in .net core?

This is my first angular application. I used following link to start https://blogs.msdn.microsoft.com/webdev/2017/02/14/building-single-page-applications-on-asp-net-core-with-javascriptservices/

I want to use windows authentication, to get login id in a controller.

Community
  • 1
  • 1
Mandar Patil
  • 538
  • 2
  • 10
  • 29

5 Answers5

14

That really depends on what kind of authentication you have in your app.

Considering you mentioned Angular, I'm not sure what framework you use for authentication, and what are your settings.

To get you moving into the right direction, your course of action would be getting the relevant claim from the user identity. Something like this:

var ident = User.Identity as ClaimsIdentity;
var userID = ident.Claims.FirstOrDefault(c => c.Type == idClaimType)?.Value;

where idClaimType is the type of the claim that stores your Identity. Depending on the framework, it would usually either be
ClaimTypes.NameIdentifier (= "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")
or
JwtClaimTypes.Subject (= "sub")

If you're using Asp.Net Core Identity, you could use their helper method on the UserManager to simplify access to it:

UserManager<ApplicationUser> _userManager;
[…]
var userID = _userManager.GetUserId(User);
Artiom Chilaru
  • 11,811
  • 4
  • 41
  • 52
  • HI...GetUserId(); it is not supported in Core 2.0... I am using var userID = ident.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; and have following error... "Evaluation of method System.Linq.Enumerable.FirstOrDefault() calls into native method Interop+Kernel32.GetCurrentThread(). Evaluation of native methods in this context is not supported." any idea? Thanks – Diego Mar 28 '18 at 14:46
  • @Diego actually, you're wrong. `GetUserId()` is an extension method in `Microsoft.AspNet.Identity.Core, Version=2.0.0.0`, as explained in my post. – Artiom Chilaru May 01 '18 at 09:24
  • 1
    `Microsoft.AspNet.Identity.Core` is not a .NET Core library. So you should not use it in a .NET Core project. See https://stackoverflow.com/questions/41140952/what-is-the-difference-between-microsoft-aspnet-identity-core-vs-microsoft-aspne The right library to use is `Microsoft.AspNetCore.Identity` and that doesn't have that extention so you have to write it yourself. – MonkeyDreamzzz Jul 03 '18 at 14:02
  • @Rubanov whoops.. You're absolutely right! Not sure how that slipped in there =/ – Artiom Chilaru Aug 15 '18 at 17:57
3

Assuming you are using ASP.NET Identity, what about (in your controller action):

User.Identity.GetUserId();

or (if you use a custom key type)

User.Identity.GetUserId<TKey>();
Omar Muscatello
  • 1,256
  • 14
  • 25
  • 2
    This really depends on what kind of authentication he's using. This is an extension method from `Microsoft.AspNet.Identity.Core`, and he might not even be using it – Artiom Chilaru Sep 18 '17 at 11:25
  • You are righe @ArtiomChilaru. Anyway, I assumed he is using the default approach because he not specified it. Answer edited. – Omar Muscatello Sep 19 '17 at 06:10
2

Try this one:

var user = await userManager.GetUserAsync(HttpContext.User);
var ID = user.Id;
Dmitry
  • 6,716
  • 14
  • 37
  • 39
Saad Hasan
  • 458
  • 5
  • 17
2
private readonly ApplicationDbContext _dbContext;
private readonly IHttpContextAccessor _httpContext;
private readonly string _userId;

public MyController(UserManager<ApplicationUser> _userManager, IHttpContextAccessor _httpContext)
{
    this._userManager = _userManager;
    this._httpContext = _httpContext;

    _userId = this._userManager.GetUserId(this._httpContext.HttpContext.User);
}
Andrew Diamond
  • 6,295
  • 1
  • 15
  • 33
Ian Foster
  • 29
  • 3
1

I use this one :

    private UserManager<ApplicationUser> _userManager;

    public ClassConstructor(UserManager<ApplicationUser> userManager)
    {
        _userManager = userManager;
    }

    public void OnGet()
    {
        var id = _userManager.GetUserId(User);
    }

p.s. u can find more useful method inside UserManager.

Ertan Hasani
  • 797
  • 1
  • 18
  • 37