|
@@ -1,14 +1,23 @@
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
|
using System.Text;
|
|
|
+using Mapster;
|
|
|
+using MapsterMapper;
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
+using Microsoft.OpenApi.Models;
|
|
|
using XF.Domain.Options;
|
|
|
|
|
|
namespace Hotline.Api
|
|
|
{
|
|
|
public static class StartupHelper
|
|
|
{
|
|
|
- public static IServiceCollection AddAuthenticationService(this IServiceCollection services, ConfigurationManager configuration)
|
|
|
+ /// <summary>
|
|
|
+ /// Authentication
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="services"></param>
|
|
|
+ /// <param name="configuration"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static IServiceCollection RegisterAuthentication(this IServiceCollection services, ConfigurationManager configuration)
|
|
|
{
|
|
|
//JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
|
|
|
//services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
@@ -55,10 +64,126 @@ namespace Hotline.Api
|
|
|
ValidateIssuerSigningKey = true,
|
|
|
IssuerSigningKey = secKey,
|
|
|
};
|
|
|
+
|
|
|
+ //d.Audience = "hotline_api";
|
|
|
+ d.Events = new JwtBearerEvents
|
|
|
+ {
|
|
|
+ OnMessageReceived = context =>
|
|
|
+ {
|
|
|
+ var accessToken = context.Request.Query["access_token"];
|
|
|
+
|
|
|
+ // If the request is for our hub...
|
|
|
+ var path = context.HttpContext.Request.Path;
|
|
|
+ if (!string.IsNullOrEmpty(accessToken) &&
|
|
|
+ (path.StartsWithSegments("/hubs/callcenter")))
|
|
|
+ {
|
|
|
+ // Read the token out of the query string
|
|
|
+ context.Token = accessToken;
|
|
|
+ }
|
|
|
+ return Task.CompletedTask;
|
|
|
+ }
|
|
|
+ };
|
|
|
})
|
|
|
;
|
|
|
|
|
|
return services;
|
|
|
}
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Swagger
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="services"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static IServiceCollection RegisterSwagger(this IServiceCollection services)
|
|
|
+ {
|
|
|
+ services.AddSwaggerGen(c =>
|
|
|
+ {
|
|
|
+ //添加文档
|
|
|
+ c.SwaggerDoc("v1", new OpenApiInfo() { Title = "Hotline Api", Version = "v1.0", Description = "城市热线api" });
|
|
|
+ //使用反射获取xml文件,并构造出文件的路径
|
|
|
+ var xmlFile = "document.xml";
|
|
|
+ //var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
|
+ var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
|
|
+ // 启用xml注释. 该方法第二个参数启用控制器的注释,默认为false.
|
|
|
+ c.IncludeXmlComments(xmlPath, true);
|
|
|
+
|
|
|
+ var scheme = new OpenApiSecurityScheme()
|
|
|
+ {
|
|
|
+ Description = "Authorization header. \r\nExample: 'Bearer ***'",
|
|
|
+ Reference = new OpenApiReference
|
|
|
+ {
|
|
|
+ Type = ReferenceType.SecurityScheme,
|
|
|
+ Id = "Authorization"
|
|
|
+ },
|
|
|
+ Scheme = "oauth2",
|
|
|
+ Name = "Authorization",
|
|
|
+ In = ParameterLocation.Header,
|
|
|
+ Type = SecuritySchemeType.ApiKey,
|
|
|
+ };
|
|
|
+ c.AddSecurityDefinition("Authorization", scheme);
|
|
|
+ var requirement = new OpenApiSecurityRequirement();
|
|
|
+ requirement[scheme] = new List<string>();
|
|
|
+ c.AddSecurityRequirement(requirement);
|
|
|
+ });
|
|
|
+
|
|
|
+ return services;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Cors
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="services"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static IServiceCollection RegisterCors(this IServiceCollection services, ConfigurationManager configuration, string corsOrigins)
|
|
|
+ {
|
|
|
+ services.AddCors(options =>
|
|
|
+ {
|
|
|
+ options.AddPolicy(name: corsOrigins,
|
|
|
+ builder =>
|
|
|
+ {
|
|
|
+ var origins = configuration.GetSection("Cors:Origins").Get<string[]>();
|
|
|
+ builder.SetIsOriginAllowed(a =>
|
|
|
+ {
|
|
|
+ return origins.Any(origin => origin.StartsWith("*.", StringComparison.Ordinal)
|
|
|
+ ? a.EndsWith(origin[1..], StringComparison.Ordinal)
|
|
|
+ : a.Equals(origin, StringComparison.Ordinal));
|
|
|
+ })
|
|
|
+ .AllowAnyHeader()
|
|
|
+ .AllowAnyMethod()
|
|
|
+ .AllowCredentials();
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ return services;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Mapper
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="services"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static IServiceCollection RegisterMapper(this IServiceCollection services)
|
|
|
+ {
|
|
|
+ var config = TypeAdapterConfig.GlobalSettings;
|
|
|
+ services.AddSingleton(config);
|
|
|
+ services.AddScoped<IMapper, ServiceMapper>();
|
|
|
+
|
|
|
+ return services;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// SignalR
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="services"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static IServiceCollection RegisterSignalR(this IServiceCollection services, ConfigurationManager configuration)
|
|
|
+ {
|
|
|
+ services.AddSignalR().AddStackExchangeRedis(configuration.GetConnectionString("Redis"), options =>
|
|
|
+ {
|
|
|
+ options.Configuration.ChannelPrefix = "callcenter:signalR:";
|
|
|
+ });
|
|
|
+
|
|
|
+ return services;
|
|
|
+ }
|
|
|
}
|
|
|
}
|