source: iknow-api/Program.cs@ ff202cf

Last change on this file since ff202cf 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
Line 
1using System.Text;
2using System.Text.Json.Serialization;
3using iknow_api.Data;
4using iknow_api.Repositories;
5using iknow_api.Services;
6using Microsoft.AspNetCore.Authentication.JwtBearer;
7using Microsoft.EntityFrameworkCore;
8using Microsoft.IdentityModel.Tokens;
9
10var builder = WebApplication.CreateBuilder(args);
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});
35// Add services to the container.
36builder.Services.AddControllers()
37 .AddJsonOptions(options =>
38 {
39 options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
40 });
41builder.Services.AddOpenApi();
42
43// Register DbContext
44builder.Services.AddDbContext<AppDbContext>(options =>
45 options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
46
47// Register your services
48builder.Services.AddScoped<IAuthService, AuthService>();
49builder.Services.AddScoped<IUserRepository, UserRepository>(); // You'll need to add the UserRepository implementation
50builder.Services.AddScoped<IUserService, UserService>();
51
52builder.Services.AddScoped<IRefreshTokenService, RefreshTokenService>();
53builder.Services.AddScoped<IUserRepository, UserRepository>();
54builder.Services.AddScoped<IRefreshTokenRepository, RefreshTokenRepository>();
55builder.Services.AddAuthorization();
56var app = builder.Build();
57
58// Configure the HTTP request pipeline.
59if (app.Environment.IsDevelopment())
60{
61 app.MapOpenApi();
62}
63
64app.UseHttpsRedirection();
65app.UseAuthentication(); // <-- must come BEFORE UseAuthorization
66app.UseAuthorization();
67app.MapControllers();
68
69app.Run();
Note: See TracBrowser for help on using the repository browser.