Files
Web_SupplierManager_Prod/SupplierManager/Program.cs
2025-11-20 09:14:00 +08:00

109 lines
4.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Text;
using System.Threading.RateLimiting;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.RateLimiting;
using Microsoft.IdentityModel.Tokens;
using SupplierManager.Common;
using SupplierManager.Models;
namespace SupplierManager
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddScoped<AgentApprovalSystemContext>();
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.AddRateLimiter(options =>
//{
// options.AddTokenBucketLimiter(policyName: "token_bucket", tokenBucketOptions =>
// {
// tokenBucketOptions.TokenLimit = 100;//桶最多可以装的令牌数,发放的多余令牌会被丢弃
// tokenBucketOptions.ReplenishmentPeriod = TimeSpan.FromSeconds(10);//令牌发放周期
// tokenBucketOptions.TokensPerPeriod = 100;//每个周期发放令牌数
// tokenBucketOptions.QueueLimit = 90;//当桶内的令牌全部被拿完token=0后续请求会进入排队
// tokenBucketOptions.QueueProcessingOrder = QueueProcessingOrder.OldestFirst;
// tokenBucketOptions.AutoReplenishment = true;//进入新令牌发放周期是否自动发放令牌。如果设置为false则需要手动调用 TokenBucketRateLimiter.TryReplenish来发放
// });
//});
builder.Services.AddAuthorization();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(option =>
{
string DefaultKey = "B,EZipeApY3cNj3~4RP0UMR=H>9x8.1!E85wmZ]]py2d$Y?5";
var sec = Encoding.UTF8.GetBytes(builder.Configuration["JWT:SecretKey"] ?? DefaultKey);
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))
{
// 如果没有找到 token 头部,则继续检查 Authorization 头部
token = context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();
}
// 如果找到了 token则将其设置到 HttpContext 中
if (!string.IsNullOrEmpty(token))
{
context.Token = token;
}
return Task.CompletedTask;
}
};
});
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseCors("Vue3");
app.UseAuthentication(); // 添加认证中间件
app.UseAuthorization(); // 使用授权中间件
app.UseStaticFiles();
app.MapControllers();
//app.UseRateLimiter(new Microsoft.AspNetCore.RateLimiting.RateLimiterOptions()
//{
// RejectionStatusCode = 500
//});
StaticData.GetWebAPIMethod();
app.Run();
}
}
}