初始化
This commit is contained in:
89
WebAPIServer/Program.cs
Normal file
89
WebAPIServer/Program.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Text;
|
||||
using WebAPIServer.Common;
|
||||
|
||||
namespace WebAPIServer
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public record STU(string nnn, string bbb);
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddMemoryCache();
|
||||
builder.Services.AddControllers();
|
||||
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy(name: "Vue3",
|
||||
policy =>
|
||||
{
|
||||
//policy.WithOrigins("http://localhost:5180",
|
||||
// "http://localhost:8809/",
|
||||
// "http://www.contoso.com",
|
||||
// "http://new.uts-data.com:6688/", "http://new.uts-data.com")
|
||||
policy
|
||||
.AllowAnyOrigin()
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod();
|
||||
});
|
||||
});
|
||||
|
||||
builder.Services.AddAuthorization();
|
||||
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(option =>
|
||||
{
|
||||
var sec = Encoding.UTF8.GetBytes(builder.Configuration["JWT:SecretKey"]);
|
||||
|
||||
option.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidIssuer = builder.Configuration["JwT:Issuer"],
|
||||
ValidAudience = builder.Configuration["JwT:Audience"],
|
||||
IssuerSigningKey = new SymmetricSecurityKey(sec)
|
||||
};
|
||||
|
||||
//option.Events = new JwtBearerEvents
|
||||
//{
|
||||
// OnMessageReceived = context =>
|
||||
// {
|
||||
// var token = context.Request.Headers["token"].FirstOrDefault();
|
||||
// if (string.IsNullOrEmpty(token))
|
||||
// {
|
||||
// // <20><><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>ҵ<EFBFBD> token ͷ<><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Authorization ͷ<><CDB7>
|
||||
// token = context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();
|
||||
// }
|
||||
// // <20><><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD> token<65><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD> HttpContext <20><>
|
||||
// if (!string.IsNullOrEmpty(token))
|
||||
// {
|
||||
// context.Token = token;
|
||||
// }
|
||||
// return Task.CompletedTask;
|
||||
// }
|
||||
//};
|
||||
});
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
app.UseCors("Vue3");
|
||||
|
||||
app.UseAuthentication(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD>м<EFBFBD><D0BC><EFBFBD>
|
||||
app.UseAuthorization(); // ʹ<><CAB9><EFBFBD><EFBFBD>Ȩ<EFBFBD>м<EFBFBD><D0BC><EFBFBD>
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
StaticData.GetWebAPIMethod();
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user