AuthConfigurer.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Text;
  3. using Microsoft.Extensions.Configuration;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Microsoft.IdentityModel.Tokens;
  6. namespace VberAdmin.Web.Startup;
  7. public static class AuthConfigurer
  8. {
  9. public static void Configure(IServiceCollection services, IConfiguration configuration)
  10. {
  11. if (bool.Parse(configuration["Authentication:JwtBearer:IsEnabled"]))
  12. {
  13. services.AddAuthentication()
  14. .AddJwtBearer(options =>
  15. {
  16. options.Audience = configuration["Authentication:JwtBearer:Audience"];
  17. options.TokenValidationParameters = new TokenValidationParameters
  18. {
  19. // The signing key must match!
  20. ValidateIssuerSigningKey = true,
  21. IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(configuration["Authentication:JwtBearer:SecurityKey"])),
  22. // Validate the JWT Issuer (iss) claim
  23. ValidateIssuer = true,
  24. ValidIssuer = configuration["Authentication:JwtBearer:Issuer"],
  25. // Validate the JWT Audience (aud) claim
  26. ValidateAudience = true,
  27. ValidAudience = configuration["Authentication:JwtBearer:Audience"],
  28. // Validate the token expiry
  29. ValidateLifetime = true,
  30. // If you want to allow a certain amount of clock drift, set that here
  31. ClockSkew = TimeSpan.Zero
  32. };
  33. });
  34. }
  35. }
  36. }