I am trying to find a way to get a UserId
string to my service layer without having to modify the request passed from the FE.
I am using Auth0 to authenticate users for an application. I use middleware to access the users id and set it to the context.items
enumerable using this code:
app.Use(async (context, next) =>
{
var userIdClaim = context.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier);
if (userIdClaim is not null)
{
context.Items["UserId"] = userIdClaim.Value;
}
await next.Invoke();
});
And this works fine, so I get get the UserId
in my controller using
(string)HttpContext.Items["UserId"];
Currently, I modified the request object received from the front end to include a nullable string UserId
so I can pass it along with this code:
[HttpGet]
public async Task<AddressResponse.GetIndex> GetIndexAsync([FromQuery] AddressRequest.GetIndex request)
{
request.UserId = (string)HttpContext.Items["UserId"];
return await addressService.GetIndexAsync(request);
}
public class AddressRequest
{
public class GetIndex : AuthenticatedData {}
}
public class AuthenticatedData
{
public string? UserId { get; set; }
}
Now for my detail and delete functions I only pass a primitive int to the route/body so I cannot modify it to include a UserId
. Should I make every request an object with UserId
even if it seems a bit overkill? Or is there another way to receive the UserId
in the service layer?
I am using a shared interface between my Blazor app and the api, so I cannot just modify the interface to include a UserId
(I think).
And is there a way to set the request UserId
in the middleware itself and not with every controller function?