-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
127 lines (110 loc) · 3.9 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System.Text;
using AllInOneAspNet.Controllers;
using AllInOneAspNet.Endpoints;
using AllInOneAspNet.Models.ClientModels;
using AllInOneAspNet.Models.UserModels;
using AllInOneAspNet.Repositories;
using AllInOneAspNet.Repositories.Contexts;
using AllInOneAspNet.Services.Jwt;
using AllInOneAspNet.Validators.ClientValidators;
using AllInOneAspNet.Validators.UserValidators;
using FluentValidation;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Serilog;
using ILogger = Serilog.ILogger;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
ILogger logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
builder.Logging.ClearProviders();
builder.Logging.AddSerilog(logger);
builder.Host.UseSerilog(logger);
builder.Services.AddDbContext<DatabaseContext>(options =>
{
const string connectionString = @"Data Source=AllInOneDatabase.db;";
options.UseSqlite(connectionString);
});
builder.Services.AddScoped<UserRepository>();
builder.Services.AddScoped<ClientRepository>();
builder.Services.AddScoped<IValidator<UserSigninRequestModel>, UserSigninRequestValidator>();
builder.Services.AddScoped<IValidator<UserLoginRequestModel>, UserLoginRequestValidator>();
builder.Services.AddScoped<IValidator<ClientRegisterRequestModel>, ClientRegisterRequestValidator>();
builder.Services.AddScoped<IValidator<ClientUpdateRequestModel>, ClientUpdateRequestValidator>();
builder.Services.AddScoped<UserController>();
builder.Services.AddScoped<ClientController>();
builder.Services.AddSingleton<JwtService>(_ =>
{
byte[] byteKey = Encoding.UTF8.GetBytes(JwtConsts.JWT_SIMETRIC_KEY_SHA256);
return new(byteKey);
});
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
byte[] byteKey = Encoding.UTF8.GetBytes(JwtConsts.JWT_SIMETRIC_KEY_SHA256);
options.TokenValidationParameters = new()
{
ValidateIssuer = true,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(byteKey),
ValidIssuer = JwtConsts.JWT_ISSUER,
ValidateAudience = false
};
});
builder.Services.AddAuthorization();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new()
{
Title = "AllInOneAspNet",
Version = "v1",
Description = "Web API example with ASP.NET Core"
});
options.AddSecurityDefinition("Bearer", new()
{
Name = "Authorization",
Type = SecuritySchemeType.Http,
Scheme = "Bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "JWT Authorization header using the Bearer scheme."
});
options.AddSecurityRequirement(new()
{
{
new()
{
Reference = new()
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
});
WebApplication app = builder.Build();
using (IServiceScope scope = app.Services.CreateScope())
{
DatabaseContext dbContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
dbContext.Database.EnsureCreated();
}
app.UseAuthentication();
app.UseAuthorization();
app.UseSwagger();
app.UseSwaggerUI();
RouteGroupBuilder userGroup = app.MapGroup("user")
.AllowAnonymous();
userGroup.MapUserEndpoints();
RouteGroupBuilder clientGroup = app.MapGroup("client")
.RequireAuthorization(policyBuilder => policyBuilder.RequireClaim(JwtConsts.CLAIM_ID));
clientGroup.MapClientEndpoints();
app.Run();