Reputation: 55
I'm trying to get the id of the current user (the user executing the method) with Identity package. The context is a UserManager<ApplicationUser> (ApplicationUser is an IdentityUser).
var currentUserId = User.FindFirst(ClaimTypes.NameIdentifier).Value;
I add this to the list of claims when a user logs in. 'user' is the ApplicationUser from the DB.
new Claim(ClaimTypes.NameIdentifier, user.Id),
I also used this doc and added the following in Program.cs.
builder.Services.Configure<IdentityOptions>(options =>
options.ClaimsIdentity.UserIdClaimType = ClaimTypes.NameIdentifier);
However, I keep getting an "System.NullReferenceException: 'Object reference not set to an instance of an object.'" error.
The link also showed that I could get the current user as
var user = await userManager.GetUserAsync(User);
However, that just made the user null.
I'm using postman/swagger for debugging.
Here is the entire stack trace:
System.NullReferenceException: Object reference not set to an instance of an object.
at Server.Authentication.Controllers.AuthController.UpdateUser(String id, UpdateModel model) in C:\i2\PromoCreate\promocreate\Server\Authentication\Controllers\AuthController.cs:line 168
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Can someone tell me what I'm doing wrong?
Upvotes: 2
Views: 1209
Reputation: 55
I'm answering my own question. I simply needed the [Authorize] attribute above the method.
Upvotes: 3