Spaces:
Sleeping
Sleeping
| using System.Text; | |
| using Database.Models; | |
| using LibraryManagement.Backend.Features.Auth; | |
| using LibraryManagement.Backend.Features.Users; | |
| using LibraryManagement.Backend.Features.Books; | |
| using Microsoft.AspNetCore.Authentication.JwtBearer; | |
| using Microsoft.EntityFrameworkCore; | |
| using Microsoft.IdentityModel.Tokens; | |
| using Microsoft.OpenApi.Models; | |
| using LibraryManagement.Backend.Features.Subscriptions; | |
| using LibraryManagement.Backend.Features.Categories; | |
| using LibraryManagement.Backend.Features.Borrowings; | |
| var builder = WebApplication.CreateBuilder(args); | |
| builder.Services.AddControllers(); | |
| builder.Services.AddAuthorization(); | |
| builder.Services.AddEndpointsApiExplorer(); | |
| builder.Services.AddSwaggerGen(c => | |
| { | |
| c.SwaggerDoc("v1", new OpenApiInfo { Title = "Library Management API", Version = "v1" }); | |
| c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme | |
| { | |
| Description = "JWT Authorization header using the Bearer scheme.", | |
| Name = "Authorization", | |
| In = ParameterLocation.Header, | |
| Type = SecuritySchemeType.Http, | |
| Scheme = "bearer", | |
| BearerFormat = "JWT" | |
| }); | |
| c.AddSecurityRequirement(new OpenApiSecurityRequirement | |
| { | |
| { | |
| new OpenApiSecurityScheme | |
| { | |
| Reference = new OpenApiReference | |
| { | |
| Type = ReferenceType.SecurityScheme, | |
| Id = "Bearer" | |
| } | |
| }, | |
| new string[] {} | |
| } | |
| }); | |
| }); | |
| builder.Services.AddDbContext<LibraryManagementContext>(options => | |
| options.UseSqlServer(builder.Configuration.GetConnectionString("MssqlConnection"))); | |
| // Register Services | |
| builder.Services.AddScoped<IAuthService, AuthService>(); | |
| builder.Services.AddScoped<IUserService, UserService>(); | |
| builder.Services.AddScoped<IBookService, BookService>(); | |
| builder.Services.AddScoped<ISubscriptionService, SubscriptionService>(); | |
| builder.Services.AddScoped<ICategoryService, CategoryService>(); | |
| builder.Services.AddScoped<IBorrowingService, BorrowingService>(); | |
| // Configure JWT Authentication | |
| var key = Encoding.ASCII.GetBytes(builder.Configuration["Jwt:Secret"] ?? "YourSuperSecretKeyForLibraryManagementSystem_AtLeast32CharsLong"); | |
| builder.Services.AddAuthentication(x => | |
| { | |
| x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; | |
| x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; | |
| }) | |
| .AddJwtBearer(x => | |
| { | |
| x.RequireHttpsMetadata = false; | |
| x.SaveToken = true; | |
| x.TokenValidationParameters = new TokenValidationParameters | |
| { | |
| ValidateIssuerSigningKey = true, | |
| IssuerSigningKey = new SymmetricSecurityKey(key), | |
| ValidateIssuer = true, | |
| ValidateAudience = true, | |
| ValidIssuer = builder.Configuration["Jwt:Issuer"], | |
| ValidAudience = builder.Configuration["Jwt:Audience"], | |
| ClockSkew = TimeSpan.Zero | |
| }; | |
| }); | |
| var app = builder.Build(); | |
| app.UseSwagger(); | |
| app.UseSwaggerUI(); | |
| app.UseAuthentication(); | |
| app.UseAuthorization(); | |
| app.MapControllers(); | |
| app.Run(); | |