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.

1
  • I don't see any authentication set up for logging in. What are you using for logging the user in? Commented Sep 18, 2017 at 15:29

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

4 Comments

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 actually, you're wrong. GetUserId() is an extension method in Microsoft.AspNet.Identity.Core, Version=2.0.0.0, as explained in my post.
Microsoft.AspNet.Identity.Core is not a .NET Core library. So you should not use it in a .NET Core project. See stackoverflow.com/questions/41140952/… The right library to use is Microsoft.AspNetCore.Identity and that doesn't have that extention so you have to write it yourself.
@Rubanov whoops.. You're absolutely right! Not sure how that slipped in there =/
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>();

2 Comments

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
You are righe @ArtiomChilaru. Anyway, I assumed he is using the default approach because he not specified it. Answer edited.
2

Try this one:

var user = await userManager.GetUserAsync(HttpContext.User);
var ID = user.Id;

Comments

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);
}

Comments

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.

2 Comments

injecting UserManager as you have in your Answer, this worked for me: var id = _userManager.Users.First();
@StackOverflowUser that just grabs the first user, regardless of whether it's the current user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.