source: iknow-api/Program.cs@ 1e58098

Last change on this file since 1e58098 was 4e061d8, checked in by imbrsk <boris696boris@…>, 11 months ago

Implement refresh token functionality with associated services and repositories

  • Property mode set to 100644
File size: 1.9 KB
RevLine 
[5eb7847]1using System.Text;
2using iknow_api.Data;
[271178d]3using iknow_api.Repositories;
[5eb7847]4using iknow_api.Services;
5using Microsoft.AspNetCore.Authentication.JwtBearer;
6using Microsoft.EntityFrameworkCore;
7using Microsoft.IdentityModel.Tokens;
[618be76]8
[c11d3a3]9var builder = WebApplication.CreateBuilder(args);
[5eb7847]10builder.Services.AddAuthentication(options =>
11{
12 options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
13 options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
14})
15.AddJwtBearer(options =>
16{
17 options.RequireHttpsMetadata = false; // only for development
18 options.SaveToken = true;
19 options.TokenValidationParameters = new TokenValidationParameters
20 {
21 ValidateIssuer = true,
22 ValidIssuer = "iknow-api",
23
24 ValidateAudience = true,
25 ValidAudience = "iknow-api",
26
27 ValidateLifetime = true,
28
29 ValidateIssuerSigningKey = true,
30 IssuerSigningKey = new SymmetricSecurityKey(
31 Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
32 };
33});
[c11d3a3]34// Add services to the container.
35builder.Services.AddControllers();
36builder.Services.AddOpenApi();
37
[fc7b4b4]38// Register DbContext
[c0a75ab]39builder.Services.AddDbContext<AppDbContext>(options =>
40 options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
41
[fc7b4b4]42// Register your services
43builder.Services.AddScoped<IAuthService, AuthService>();
[4e061d8]44builder.Services.AddScoped<IRefreshTokenService, RefreshTokenService>();
45builder.Services.AddScoped<IUserRepository, UserRepository>();
46builder.Services.AddScoped<IRefreshTokenRepository, RefreshTokenRepository>();
[5eb7847]47builder.Services.AddAuthorization();
[c11d3a3]48var app = builder.Build();
49
50// Configure the HTTP request pipeline.
51if (app.Environment.IsDevelopment())
52{
53 app.MapOpenApi();
54}
55
56app.UseHttpsRedirection();
[5eb7847]57app.UseAuthentication(); // <-- must come BEFORE UseAuthorization
[c11d3a3]58app.UseAuthorization();
59app.MapControllers();
60
61app.Run();
Note: See TracBrowser for help on using the repository browser.