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

context.CurrentUserId always null #40

Closed moutono closed 6 years ago

moutono commented 6 years ago

Hi. I noticed that context.CurrentUserId always returns null. It seems the httpAccessor in the HttpUnitOfWork class is instantiated but it doesn't actually return the current user. Hence, CreatedBy and UpdatedBy are never updated.

I've tried to find a solution but it seems to me that after updating to the latest libraries this way of getting the current user, outside a controller, does not work. It always returns null. Any suggestions? Thanks.

wmlockhart commented 6 years ago

In HttpUnitOfWork.cs CurrentUserId is set to the value Claims.Subject:

context.CurrentUserId = httpAccessor.HttpContext.User.FindFirst(OpenIdConnectConstants.Claims.Subject)?.Value?.Trim();

I'm away from my desk, so I can't check this myself, but you could check (if you haven't done so already) to see what value is assigned to Claims.Subject in AuthorizationController.cs:

public async Task Exchange(OpenIdConnectRequest request)

Just a suggestion. I can't try this myself as I'm out-and-about.

moutono commented 6 years ago

The value "sub" is assigned to Claims.Subject. Im getting the userid in a controller with this: var id = User.FindFirst(OpenIdConnectConstants.Claims.Subject).Value;

But I liked the pattern where this code was moved to the context to update the CreatedBy and UpdatedBy of AuditableEntity. It seems to me the IHttpContextAccessor httpAccessor is not injected correctly.

I have a workaround so this isn't holding me up. Just wanted to understand what's happening.

wmlockhart commented 6 years ago

Would you mind sharing your workaround please

moutono commented 6 years ago

Sure. Like I said in the previous post you can get the UserId in a controller using this: var userId = User.FindFirst(OpenIdConnectConstants.Claims.Subject).Value;

So when I add an entity in a controller I do this: var userId = User.FindFirst(OpenIdConnectConstants.Claims.Subject).Value; entity.CreatedBy = userId; entity.UpdatedBy = userId; ... _context.SaveChanges();

And when I update an entity in the controller I do this: var userId = User.FindFirst(OpenIdConnectConstants.Claims.Subject).Value; entity.UpdatedBy = userId; ... _context.SaveChanges();

This works but is not as elegant as the original QuickApp solution of getting the current logged in user inside the context.

wmlockhart commented 6 years ago

Thank you.

emonney commented 6 years ago

Note that CurrentUserId is only available when a user is logged in (i.e. when an authentication token is available in the request header). Do share a project with this issue or the steps to reproduce this in the current repository.

emonney commented 6 years ago

Closing issue. If this is still a problem feel free to reopen