-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
114 lines (105 loc) · 4.52 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
using MemosService.Data;
using MemosService.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using System.Text;
namespace MemosService
{
class Program
{
static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseUrls("http://*:5046");
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddCors(option =>
{
option.AddPolicy("AllowSpecificOrigin", policy =>
{
policy.WithOrigins(builder.Configuration["Cors:domain"]!)
.AllowAnyHeader()
.SetIsOriginAllowedToAllowWildcardSubdomains()
.AllowAnyMethod();
});
});
builder.Services.AddSwaggerGen(option =>
{
option.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "Memos API",
Description = "An ASP.NET Core Web API for managing memos",
License = new OpenApiLicense
{
Name = "License",
Url = new Uri("https://mit-license.org/")
}
});
var basePath = AppContext.BaseDirectory;
var xmlPath = Path.Combine(basePath, "MemosService.xml");
option.IncludeXmlComments(xmlPath, true);
// 文档页添加标头
var scheme = new OpenApiSecurityScheme()
{
Name = "Authorization",
Description = "Authorization header \r\n 输入 JWT 值",
Reference = new OpenApiReference()
{
Type = ReferenceType.SecurityScheme,
Id = "Authorization",
},
Scheme = "Bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
};
option.AddSecurityDefinition("Authorization", scheme);
var requirement = new OpenApiSecurityRequirement();
requirement[scheme] = new List<string>();
option.AddSecurityRequirement(requirement);
});
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["JWT:Issuer"],
ValidAudience = builder.Configuration["JWT:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JWT:Key"]!)),
// Token 过期时间为 1 年
LifetimeValidator = (before, expires, token, param) => expires > DateTime.UtcNow,
ClockSkew = TimeSpan.Zero,
};
});
builder.Services.AddDbContext<MemosContext>(options =>
{
options.UseSqlite(builder.Configuration.GetConnectionString("WebApiDatabase"));
});
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IMemoService, MemoService>();
builder.Services.AddControllers();
var app = builder.Build();
DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions();
defaultFilesOptions.DefaultFileNames.Clear();
defaultFilesOptions.DefaultFileNames.Add("index.html");
// app.UseDeveloperExceptionPage();
app.UseDefaultFiles(defaultFilesOptions);
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCors("AllowSpecificOrigin");
app.UseSwagger();
app.UseSwaggerUI();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
}