-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
147 lines (128 loc) · 5.4 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.IdentityModel.Tokens;
using Microsoft.AspNetCore.Identity;
using System.Text;
using System.Security.Claims;
using System.Net;
using Microsoft.OpenApi.Models;
using System.Reflection;
var allowSpecificOrigins = "_allowSpecificOrigins";
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>
{
options.AddPolicy(name: allowSpecificOrigins,
policy =>
{
policy.AllowAnyOrigin();
policy.AllowAnyHeader();
policy.AllowAnyMethod();
});
});
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c => {
var scheme = new OpenApiSecurityScheme()
{
Description = "Authorization header. \r\nExample: 'Bearer mytoken'",
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Authorization"
},
Scheme = "oauth2",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
};
c.AddSecurityDefinition("Authorization", scheme);
var requirement = new OpenApiSecurityRequirement();
requirement[scheme] = new List<string>();
c.AddSecurityRequirement(requirement);
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "立交桥 Server API",
Description = "An ASP.NET Core Web API for 立交桥",
Contact = new OpenApiContact
{
Name = "Github",
Url = new Uri("https://github.com/Green-Hair/Server")
},
License = new OpenApiLicense
{
Name = "License",
Url = new Uri("https://github.com/Green-Hair/Server/blob/main/LICENSE")
}
});
// using System.Reflection;
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});
builder.Services.AddDbContext<ServerContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("ServerContextSQLite")));
builder.Services.AddDbContext<SecurityContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("SecurityContextSQLite")));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<SecurityContext>();
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(o => o.LoginPath = new PathString("/auth/login"))
.AddJwtBearer(opts => {
opts.RequireHttpsMetadata = false;
opts.SaveToken = true;
opts.TokenValidationParameters = new TokenValidationParameters {
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.ASCII.GetBytes(builder.Configuration["jwtSecret"])
),
ValidateAudience = false,
ValidateIssuer = false,
};
opts.Events = new JwtBearerEvents {
OnTokenValidated = async ctx => {
var usrmgr = ctx.HttpContext.RequestServices.GetRequiredService<UserManager<IdentityUser>>();
var signinmgr = ctx.HttpContext.RequestServices.GetRequiredService<SignInManager<IdentityUser>>();
string? username = ctx.Principal?.FindFirst(ClaimTypes.Name)?.Value;
var idUser = await usrmgr.FindByNameAsync(username);
ctx.Principal = await signinmgr.CreateUserPrincipalAsync(idUser);
},
};
});
builder.Services.Configure<IdentityOptions>(opts => {
opts.Password.RequireLowercase = false;
opts.Password.RequiredLength = 6;
opts.Password.RequireUppercase = false;
opts.Password.RequireNonAlphanumeric = false;
opts.Password.RequireDigit = false;
opts.User.RequireUniqueEmail = true;
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Server API V1");
c.RoutePrefix = string.Empty;
});
}
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var context = services.GetRequiredService<ServerContext>();
context.Database.EnsureCreated();
var securityContext = services.GetRequiredService<SecurityContext>();
securityContext.Database.EnsureCreated();
DbInitializer.Initialize(context);
}
app.UseCors(allowSpecificOrigins);
//app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();