0

Hoping someone can help me out here. I'm new to ASP, MVC and C# but trying to build a web application to learn it. I've created the default application that creates all the controllers, views etc for users and all works well.

I've now created a class and inside that class, I need to get the userID or user name of the logged in user. I've tried injecting UserManager and SignInManager into the constructor of the class but then any controllers calling that call complain that I'm not passing in the required parameters.

This may be cheeky but if someone has a basic example of how to retrieve something like the userID from inside a class, I could probably work backwards and understand what's going on. Been searching for hours on line but can't find any examples that work.

Using VS 2015 Asp Core Net RC2

Thanks

Stephen Clay
  • 107
  • 7

2 Answers2

2

The user id exists in the HttpContext. Identity just adds helpers, but you still need the HttpContext instance, which you won't have outside of a controller. This would seem to be a prime example of an XY problem, where you're asking how to do something to solve a problem in a certain way you've decided to approach it rather than asking about how to solve the problem itself. Generally speaking, if you need the user id outside of the controller, you should simply pass the literal user id from the controller to whatever you're calling into, rather than trying to retrieve it from there.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Thanks Chris. You are probably right in that I may be taking the wrong approach to solving the problem. That for me is part of the learning process. Basically, I need access to the userId or UserName in a class that is called from two different controllers. I've now tried injecting UserManager into those controllers, using userManager.GetUserAsync(HttpContext.User).Id to get ID and passing that into the class but I'm now getting a null object reference in GetUserAsync(HttpContext.User). – Stephen Clay Nov 15 '16 at 13:39
  • The way to get the user id is `User.Identity.GetUserId()`, which requires the `Microsoft.AspNet.Identity` namespace. If you've customized the primary key for the Identity user, then you need to use the generic version of that, i.e. `User.Identity.GetUserId()` where `T` is the type of the primary key. – Chris Pratt Nov 15 '16 at 15:44
  • 1
    What if I'm using Microsoft.AspNetCore.Identity ? GetUserID() doesn't exist in that case. – Stephen Clay Nov 16 '16 at 08:38
  • Actually, just found it courtesy of http://stackoverflow.com/users/1138946/cfreitas this.User.FindFirstValue(ClaimTypes.NameIdentifier) – Stephen Clay Nov 16 '16 at 08:57
  • Struggling here with same issue, trying to obtain the UserName from my UserManager to use as a default value for parameter "UpdatedBy" in a class within my DAL model. I don't understand how you would tell the controller to send it to it, and calling up that information with your suggestion of `this.User.FindFirstValue(ClaimTypes.NameIdentifier)` doesn't work, first with this, remove that it doesn't like User; as well as it doesn't like ClaimTypes. – Edward Mar 07 '17 at 06:03
  • @Edward I think you may have commented on the wrong window. It doesn't seem to fit with the topic here. – Chris Pratt Mar 07 '17 at 06:31
  • My Google search has lead to almost everything being similar to what you wrote, as per the title I am dealing with aspnetcore and a custom class in the model folder to pull/push data to a database, is a non controller class. Also username and userId are still both a part of the applicationUser in identity – Edward Mar 07 '17 at 06:34
0

This worked for me

Check Inside Services section of this article, to see how to get instance of HttpContextAccessor (e.g. _httpContextAccessor)

Then you may get the user id just like this:

long userId = ClaimsPrincipalExtensions.GetUserId(_httpContextAccessor.HttpContext.User);

Assuming that you have already written the method ClaimsPrincipalExtensions.GetUserId, if you didn't, just check this.

Hussein Dahir
  • 395
  • 4
  • 14