|
@@ -18,8 +18,12 @@ using OASystem.API.OAMethodLib.QiYeWeChatAPI;
|
|
|
using OASystem.API.OAMethodLib.Quartz.Jobs;
|
|
|
using Microsoft.AspNetCore.Cors.Infrastructure;
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
-using OASystem.API.OAMethodLib.Hubs;
|
|
|
using SqlSugar.DistributedSystem.Snowflake;
|
|
|
+using Microsoft.AspNetCore.Http.Connections;
|
|
|
+using OASystem.API.OAMethodLib.Hub.Hubs;
|
|
|
+using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
|
+using OASystem.API.OAMethodLib.SignalR.HubService;
|
|
|
+using OASystem.API.OAMethodLib.Auth;
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
var basePath = AppContext.BaseDirectory;
|
|
@@ -49,14 +53,23 @@ builder.Services.AddControllers()
|
|
|
//options.JsonSerializerOptions.Converters.Add(new JsonConverterDecimal(0.0000M));
|
|
|
});
|
|
|
|
|
|
+// 添加授权服务
|
|
|
+builder.Services.AddMyJWTBearerAuth();
|
|
|
+
|
|
|
#region Cors
|
|
|
builder.Services.AddCors(policy =>
|
|
|
{
|
|
|
+ //policy.AddPolicy("Cors", opt => opt
|
|
|
+ //.AllowAnyOrigin()
|
|
|
+ //.AllowAnyHeader()
|
|
|
+ //.AllowAnyMethod()
|
|
|
+ //.WithExposedHeaders("X-Pagination"));
|
|
|
+
|
|
|
policy.AddPolicy("Cors", opt => opt
|
|
|
- .AllowAnyOrigin()
|
|
|
- .AllowAnyHeader()
|
|
|
- .AllowAnyMethod()
|
|
|
- .WithExposedHeaders("X-Pagination"));
|
|
|
+ .SetIsOriginAllowed(origin => true)//这个必须加
|
|
|
+ .AllowAnyHeader()
|
|
|
+ .WithMethods("GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS")
|
|
|
+ .AllowCredentials());//这个一定不能少);
|
|
|
|
|
|
});
|
|
|
#endregion
|
|
@@ -199,22 +212,22 @@ if (AppSettingsHelper.Get("UseSwagger").ToBool())
|
|
|
#region 添加校验
|
|
|
|
|
|
//builder.Services.AddTransient<OASystemAuthentication>();
|
|
|
-builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
- .AddJwtBearer(options =>
|
|
|
- {
|
|
|
- options.TokenValidationParameters = new TokenValidationParameters
|
|
|
- {
|
|
|
- ValidateIssuer = true,
|
|
|
- ValidateAudience = true,
|
|
|
- ValidateLifetime = true,
|
|
|
- ValidateIssuerSigningKey = true,
|
|
|
- ValidAudience = "OASystem.com",
|
|
|
- ValidIssuer = "OASystem.com",
|
|
|
- IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["JwtSecurityKey"])),
|
|
|
- ClockSkew = TimeSpan.FromSeconds(30), //过期时间容错值,解决服务器端时间不同步问题(秒)
|
|
|
- RequireExpirationTime = true,
|
|
|
- };
|
|
|
- });
|
|
|
+//builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
+// .AddJwtBearer(options =>
|
|
|
+// {
|
|
|
+// options.TokenValidationParameters = new TokenValidationParameters
|
|
|
+// {
|
|
|
+// ValidateIssuer = true,
|
|
|
+// ValidateAudience = true,
|
|
|
+// ValidateLifetime = true,
|
|
|
+// ValidateIssuerSigningKey = true,
|
|
|
+// ValidAudience = "OASystem.com",
|
|
|
+// ValidIssuer = "OASystem.com",
|
|
|
+// IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["JwtSecurityKey"])),
|
|
|
+// ClockSkew = TimeSpan.FromSeconds(30), //过期时间容错值,解决服务器端时间不同步问题(秒)
|
|
|
+// RequireExpirationTime = true,
|
|
|
+// };
|
|
|
+// });
|
|
|
#endregion
|
|
|
|
|
|
#region 初始化日志
|
|
@@ -284,7 +297,12 @@ builder.Services.AddSingleton<IJobFactory, IOCJobFactory>();
|
|
|
|
|
|
|
|
|
#region SignalR
|
|
|
-builder.Services.AddSignalR();
|
|
|
+builder.Services.AddSignalR()
|
|
|
+ .AddJsonProtocol(options =>
|
|
|
+ {
|
|
|
+ options.PayloadSerializerOptions.PropertyNamingPolicy = null;
|
|
|
+ });
|
|
|
+builder.Services.TryAddSingleton(typeof(CommonService));
|
|
|
#endregion
|
|
|
|
|
|
var app = builder.Build();
|
|
@@ -298,9 +316,13 @@ if (!app.Environment.IsDevelopment())
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
app.UseRouting();
|
|
|
+app.UseCors("Cors"); //Cors
|
|
|
+
|
|
|
app.UseAuthentication(); // 认证
|
|
|
app.UseAuthorization(); // 授权
|
|
|
-app.UseCors("Cors"); //Cors
|
|
|
+
|
|
|
+// 授权路径
|
|
|
+app.MapGet("generatetoken", c => c.Response.WriteAsync(JWTBearer.GenerateToken(c)));
|
|
|
|
|
|
#region 启用swaggerUI
|
|
|
if (AppSettingsHelper.Get("UseSwagger").ToBool())
|
|
@@ -339,11 +361,14 @@ app.Lifetime.ApplicationStopped.Register(() =>
|
|
|
|
|
|
|
|
|
#region SignalR
|
|
|
-app.UseEndpoints(endpoints =>
|
|
|
+
|
|
|
+app.MapHub<ChatHub>("/ChatHub", options =>
|
|
|
{
|
|
|
- endpoints.MapControllers();
|
|
|
- endpoints.MapHub<ServerHub>("/api/chatHub");
|
|
|
+ options.Transports =
|
|
|
+ HttpTransportType.WebSockets |
|
|
|
+ HttpTransportType.LongPolling;
|
|
|
});
|
|
|
+
|
|
|
#endregion
|
|
|
|
|
|
app.MapControllerRoute(
|