JWT Code

HTML/XML Detected Guest 9 Views Size: 2.22 KB Posted on: Feb 17, 26 @ 6:37 AM
  1. 1// Nuget packages
  2. 2
  3. 3// Entity-> Microsoft.EntityFrameworkCore
  4. 4// Code first ->Microsoft.EntityFrameworkCore.Tools.
  5. 5// Mysql ->pomelo mysql
  6. 6// jwt -> Microsoft.AspNetCore.Authentication.JwtBearer
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11using LibraryManagementApi.AuthService;
  12. 12using LibraryManagementApi.DBContexts;
  13. 13using LibraryManagementApi.Interface;
  14. 14using LibraryManagementApi.Repository;
  15. 15using Microsoft.AspNetCore.Authentication.JwtBearer;
  16. 16using Microsoft.IdentityModel.Tokens;
  17. 17using System.Text;
  18. 18
  19. 19var builder = WebApplication.CreateBuilder(args);
  20. 20builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
  21. 21
  22. 22// Add services to the container.
  23. 23
  24. 24builder.Services.AddControllers();
  25. 25// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  26. 26builder.Services.AddEndpointsApiExplorer();
  27. 27builder.Services.AddSwaggerGen();
  28. 28builder.Services.AddDbContext<DBContext>();
  29. 29builder.Services.AddScoped<BooksInterface, BooksRepository>();
  30. 30builder.Services.AddScoped<UserInterface, UserRepository>();
  31. 31builder.Services.AddScoped<JWTHandler>();
  32. 32builder.Services.AddCors(options =>
  33. 33{
  34. 34 options.AddPolicy("AllowAll", policy =>
  35. 35 policy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
  36. 36
  37. 37});
  38. 38
  39. 39
  40. 40builder.Services.AddAuthentication(options =>
  41. 41{
  42. 42 options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  43. 43 options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  44. 44})
  45. 45.AddJwtBearer(options =>
  46. 46{
  47. 47 options.TokenValidationParameters = new TokenValidationParameters
  48. 48 {
  49. 49 ValidateIssuer = true,
  50. 50 ValidateAudience = true,
  51. 51 ValidateLifetime = true,
  52. 52 ValidateIssuerSigningKey = true,
  53. 53 ValidIssuer = builder.Configuration["Jwt:Issuer"],
  54. 54 ValidAudience = builder.Configuration["Jwt:Issuer"],
  55. 55 IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
  56. 56 };
  57. 57});
  58. 58var app = builder.Build();
  59. 59
  60. 60// Configure the HTTP request pipeline.
  61. 61if (app.Environment.IsDevelopment())
  62. 62{
  63. 63 app.UseSwagger();
  64. 64 app.UseSwaggerUI();
  65. 65}
  66. 66
  67. 67app.UseHttpsRedirection();
  68. 68
  69. 69app.UseCors("AllowAll");
  70. 70app.UseAuthorization();
  71. 71app.UseAuthentication();
  72. 72app.MapControllers();
  73. 73
  74. 74app.Run();

Raw Paste

Comments 0
Login to post a comment.
  • No comments yet. Be the first.
Login to post a comment. Login or Register
We use cookies. To comply with GDPR in the EU and the UK we have to show you these.

We use cookies and similar technologies to keep this website functional (including spam protection via Google reCAPTCHA or Cloudflare Turnstile), and — with your consent — to measure usage and show ads. See Privacy.