|
|
@ -2,6 +2,8 @@ using Elight.Logic; |
|
|
|
using Elight.Utility; |
|
|
|
using Elight.Utility; |
|
|
|
using Microsoft.AspNetCore.Authentication; |
|
|
|
using Microsoft.AspNetCore.Authentication; |
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer; |
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer; |
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Hosting; |
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Authorization; |
|
|
|
using Microsoft.AspNetCore.Mvc.Controllers; |
|
|
|
using Microsoft.AspNetCore.Mvc.Controllers; |
|
|
|
using Microsoft.Extensions.Configuration; |
|
|
|
using Microsoft.Extensions.Configuration; |
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions; |
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions; |
|
|
@ -10,20 +12,33 @@ using Microsoft.OpenApi.Models; |
|
|
|
using SqlSugar; |
|
|
|
using SqlSugar; |
|
|
|
using System.Data; |
|
|
|
using System.Data; |
|
|
|
using System.Text; |
|
|
|
using System.Text; |
|
|
|
|
|
|
|
using System.Text.Json.Serialization; |
|
|
|
|
|
|
|
using System.Text.Json; |
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Http.Json; |
|
|
|
|
|
|
|
|
|
|
|
#region builder |
|
|
|
#region builder |
|
|
|
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args); |
|
|
|
var builder = WebApplication.CreateBuilder(args); |
|
|
|
|
|
|
|
|
|
|
|
var Configuration = builder.Configuration; |
|
|
|
var Configuration = builder.Configuration; |
|
|
|
|
|
|
|
builder.WebHost.UseUrls(Configuration.GetSection("UrlsConfiguration:Urls").Value.Split(",")); |
|
|
|
// Add services to the container. |
|
|
|
// Add services to the container. |
|
|
|
|
|
|
|
builder.Services.AddControllers().AddJsonOptions(options => |
|
|
|
builder.Services.AddControllers(); |
|
|
|
{ |
|
|
|
|
|
|
|
options.JsonSerializerOptions.PropertyNamingPolicy = null; |
|
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new Elight.Utility.NullableConverter<DateTime>()); |
|
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new Elight.Utility.NullableConverter<DateTimeOffset>()); |
|
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new Elight.Utility.NullableConverter<long>()); |
|
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new Elight.Utility.NullableConverter<int>()); |
|
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new Elight.Utility.NullableConverter<double>()); |
|
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new Elight.Utility.NullableConverter<decimal>()); |
|
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new Elight.Utility.NullableConverter<float>()); |
|
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new Elight.Utility.NullableConverter<Guid>()); |
|
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new Elight.Utility.NullableConverter<bool>()); |
|
|
|
|
|
|
|
}); |
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle |
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle |
|
|
|
builder.Services.AddEndpointsApiExplorer(); |
|
|
|
builder.Services.AddEndpointsApiExplorer(); |
|
|
|
#region |
|
|
|
#region Swagger文件 |
|
|
|
#endregion |
|
|
|
|
|
|
|
builder.Services.AddSwaggerGen(c => |
|
|
|
builder.Services.AddSwaggerGen(c => |
|
|
|
{ |
|
|
|
{ |
|
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); |
|
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); |
|
|
@ -58,9 +73,11 @@ builder.Services.AddSwaggerGen(c => |
|
|
|
Type = SecuritySchemeType.ApiKey |
|
|
|
Type = SecuritySchemeType.ApiKey |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
builder.Services.AddControllers().AddJsonOptions(options => |
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
builder.Services.AddMvc(config => |
|
|
|
{ |
|
|
|
{ |
|
|
|
options.JsonSerializerOptions.PropertyNamingPolicy = null; |
|
|
|
config.Filters.Add(new AllowAnonymousFilter()); |
|
|
|
}); |
|
|
|
}); |
|
|
|
// Ìí¼ÓÉí·ÝÑéÖ¤·þÎñ |
|
|
|
// Ìí¼ÓÉí·ÝÑéÖ¤·þÎñ |
|
|
|
builder.Services.AddAuthentication(options => |
|
|
|
builder.Services.AddAuthentication(options => |
|
|
@ -84,13 +101,13 @@ builder.Services.AddAuthentication(options => |
|
|
|
ClockSkew = TimeSpan.Zero |
|
|
|
ClockSkew = TimeSpan.Zero |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
////重点在于这里;判断是SignalR的路径(https://www.cnblogs.com/fger/p/11811190.html) |
|
|
|
//重点在于这里;判断是WebSocket的路径(https://www.cnblogs.com/fger/p/11811190.html) |
|
|
|
options.Events = new JwtBearerEvents |
|
|
|
options.Events = new JwtBearerEvents |
|
|
|
{ |
|
|
|
{ |
|
|
|
OnMessageReceived = (context) => |
|
|
|
OnMessageReceived = (context) => |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!context.HttpContext.Request.Path.HasValue) return Task.CompletedTask; |
|
|
|
if (!context.HttpContext.Request.Path.HasValue) return Task.CompletedTask; |
|
|
|
//重点在于这里;判断是SignalR的路径 |
|
|
|
//重点在于这里;判断是WebSocket的路径 |
|
|
|
var accessToken = context.HttpContext.Request.Query["access_token"]; |
|
|
|
var accessToken = context.HttpContext.Request.Query["access_token"]; |
|
|
|
var path = context.HttpContext.Request.Path; |
|
|
|
var path = context.HttpContext.Request.Path; |
|
|
|
if (string.IsNullOrWhiteSpace(accessToken) || !path.StartsWithSegments("/ws")) return Task.CompletedTask; |
|
|
|
if (string.IsNullOrWhiteSpace(accessToken) || !path.StartsWithSegments("/ws")) return Task.CompletedTask; |
|
|
@ -149,7 +166,14 @@ if (app.Environment.IsDevelopment()) |
|
|
|
app.UseSwagger(); |
|
|
|
app.UseSwagger(); |
|
|
|
app.UseSwaggerUI(); |
|
|
|
app.UseSwaggerUI(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#region websockets配置 |
|
|
|
|
|
|
|
app.UseWebSockets(new WebSocketOptions |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
KeepAliveInterval = TimeSpan.FromMinutes(2) |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
//app.UseMiddleware<WebSocketMiddleware>(); |
|
|
|
|
|
|
|
#endregion |
|
|
|
|
|
|
|
app.UseStaticFiles(); |
|
|
|
app.UseHttpsRedirection(); |
|
|
|
app.UseHttpsRedirection(); |
|
|
|
//·ÓÉ |
|
|
|
//·ÓÉ |
|
|
|
app.UseRouting(); |
|
|
|
app.UseRouting(); |
|
|
@ -168,15 +192,5 @@ app.UseSwaggerUI(c => |
|
|
|
c.RoutePrefix = string.Empty; |
|
|
|
c.RoutePrefix = string.Empty; |
|
|
|
}); |
|
|
|
}); |
|
|
|
#endregion |
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
|
|
#region websockets配置 |
|
|
|
|
|
|
|
app.UseWebSockets(new WebSocketOptions |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
KeepAliveInterval = TimeSpan.FromMinutes(120) |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
//app.UseMiddleware<WebSocketMiddleware>(); |
|
|
|
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.Run(); |
|
|
|
app.Run(); |
|
|
|
#endregion |
|
|
|
#endregion |
|
|
|