BasicAuthenticationMiddleware.cs 1.1 KB

123456789101112131415161718192021222324252627282930
  1. using iTextSharp.text.log;
  2. using Microsoft.AspNetCore.Authentication;
  3. using Microsoft.AspNetCore.Authentication.JwtBearer;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.Extensions.Logging;
  6. namespace SMP.Common.Auth
  7. {
  8. public class BasicAuthenticationMiddleware
  9. {
  10. private readonly RequestDelegate _next;
  11. public BasicAuthenticationMiddleware(RequestDelegate next)
  12. {
  13. _next = next;
  14. }
  15. public async Task Invoke(HttpContext httpContext, IAuthenticationService authenticationService)
  16. {
  17. var authenticated = await authenticationService.AuthenticateAsync(httpContext, JwtBearerDefaults.AuthenticationScheme);
  18. //authenticated.Principal.Claims.
  19. //_logger.LogInformation("Access Status:" + authenticated.Succeeded);
  20. if (!authenticated.Succeeded)
  21. {
  22. await authenticationService.ForbidAsync(httpContext, JwtBearerDefaults.AuthenticationScheme, new AuthenticationProperties { });
  23. return;
  24. }
  25. await _next(httpContext);
  26. }
  27. }
  28. }