emonney / QuickApp

ASP.NET Core / Angular startup project template with complete login, user and role management. Plus other useful services for Quick Application Development
https://www.ebenmonney.com/quickapp
MIT License
1.26k stars 594 forks source link

Can't get userID when logged in on an anonymous page #91

Closed thewebchameleon closed 5 years ago

thewebchameleon commented 6 years ago

I am trying to implement a shopping cart and I can't access the User ID from the HttpUnitOfWork class when I am on an anonymous page.

The method in question is HttpContext?.User.FindFirst(ClaimConstants.Subject) or HttpContext?.User.FindFirst(OpenIdConnectConstants.Claims.Subject)

The bearer token is being passed through on the request and when I hit a page with an Authorize attribute then I have full access to the claims.

Is this expected behaviour? Is there a way to manually get the user from the bearer token in the request header?

Owned333 commented 6 years ago

@thewebchameleon how did you solve this problem? I also try to get the current user in my controller, but I did not get it. Can you help me?

Owned333 commented 6 years ago

I Tried this, It's seems like User in HttpContext is always null, but the user is logged in. I can not understand what the problem is, help :)

2018-07-06 2:04 GMT+03:00 pomeara notifications@github.com:

Ensure you have access to the user manager (just inject it into the constructor) and then return you should be able to use it to retrieve the user from the HttpContext user i.e.

_userManager.GetUserId(HttpContext.User);

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/emonney/QuickApp/issues/91#issuecomment-402877054, or mute the thread https://github.com/notifications/unsubscribe-auth/Am-SpkYkBR5106fGbOazTJuu0yxP-ZCZks5uDpuHgaJpZM4ST88Y .

thewebchameleon commented 6 years ago

You need to ensure the action method has an Authenticate attribute for the User in HttpContext.

If you're using an action method that does not have an Authentcate attribute then I inferred the user by looking at the session ID which is passed through the HTTP headers

I ended up storing the user ID along with session ID in a table and then doing a lookup to get the User ID

I will check out injecting the UserManager today to see if that works