source: iknow-api/Program.cs@ f8af32b

Last change on this file since f8af32b was ff202cf, checked in by GitHub <noreply@…>, 11 months ago

Merge branch 'Dev' into feature/getUser

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