Moved projects to their own separate subdirectories
This commit is contained in:
41
API/YABA.API/Middlewares/AddCustomClaimsMiddleware.cs
Normal file
41
API/YABA.API/Middlewares/AddCustomClaimsMiddleware.cs
Normal file
@ -0,0 +1,41 @@
|
||||
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 userAuthProviderId = httpContext.User.Identity.GetAuthProviderId();
|
||||
|
||||
if (!string.IsNullOrEmpty(userAuthProviderId))
|
||||
{
|
||||
var userId = userService.GetUserId(userAuthProviderId);
|
||||
|
||||
if(userId <= 0)
|
||||
{
|
||||
var registedUser = await userService.RegisterUser(userAuthProviderId);
|
||||
userId = registedUser.Id;
|
||||
}
|
||||
|
||||
httpContext.User.Identities.FirstOrDefault().AddClaim(new Claim(ClaimsLookup.UserId.GetClaimName(), userId.ToString()));
|
||||
}
|
||||
}
|
||||
|
||||
await _next(httpContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
using Serilog.Context;
|
||||
using YABA.Common.Extensions;
|
||||
|
||||
namespace YABA.API.Middlewares
|
||||
{
|
||||
public class AddCustomLoggingPropertiesMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
|
||||
public AddCustomLoggingPropertiesMiddleware(RequestDelegate next)
|
||||
{
|
||||
_next = next;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext httpContext)
|
||||
{
|
||||
if(httpContext.Request.Path.HasValue && httpContext.Request.Path.Value.Contains("/api"))
|
||||
{
|
||||
LogContext.PushProperty("UserId", httpContext.User.Identity.IsAuthenticated ? httpContext.User.Identity.GetUserId() : "Anonymous");
|
||||
LogContext.PushProperty("RemoteIpAddress", httpContext.Connection.RemoteIpAddress.MapToIPv4());
|
||||
}
|
||||
|
||||
await _next(httpContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user