26 lines
737 B
C#
26 lines
737 B
C#
namespace WebApi_data_value.Authorization;
|
|
|
|
using WebApi_data_value.Services;
|
|
|
|
public class JwtMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
|
|
public JwtMiddleware(RequestDelegate next)
|
|
{
|
|
_next = next;
|
|
}
|
|
|
|
public async Task Invoke(HttpContext context, IUserService userService, IJwtUtils jwtUtils)
|
|
{
|
|
var token = context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();
|
|
var userId = jwtUtils.ValidateJwtToken(token);
|
|
if (userId != null)
|
|
{
|
|
// attach user to context on successful jwt validation
|
|
context.Items["User"] = userService.GetById(userId.Value);
|
|
}
|
|
var stop = "1";
|
|
await _next(context);
|
|
}
|
|
} |