Added UserId to claims

This commit is contained in:
Carl Tibule
2023-01-25 23:41:17 -06:00
parent 406bd9a0d1
commit afab148337
5 changed files with 63 additions and 10 deletions

View File

@ -0,0 +1,36 @@
using System.Security.Claims;
using YABA.API.Extensions;
using YABA.Common.Extensions;
using YABA.Common.Lookups;
using YABA.Service.Interfaces;
namespace YABA.API.Middlewares
{
public class AddCustomClaimsMiddleware
{
private readonly RequestDelegate _next;
public AddCustomClaimsMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext httpContext, IUserService userService)
{
if (httpContext.User != null && httpContext.User.Identity.IsAuthenticated)
{
var claims = new List<Claim>();
var userAuthProviderId = httpContext.User.Identity.GetAuthProviderId();
if (!string.IsNullOrEmpty(userAuthProviderId))
{
var userId = userService.GetUserId(userAuthProviderId);
httpContext.User.Identities.FirstOrDefault().AddClaim(new Claim(ClaimsLookup.UserId.GetClaimName(), userId.ToString()));
}
}
await _next(httpContext);
}
}
}