123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- using System.IdentityModel.Tokens.Jwt;
- using System.Reflection;
- using System.Text;
- using Hotline.Application.Orders;
- using Hotline.Identity;
- using Hotline.Repository.SqlSugar;
- using Hotline.Repository.SqlSugar.Ts;
- using Mapster;
- using MapsterMapper;
- using Microsoft.AspNetCore.Authentication.JwtBearer;
- using Microsoft.IdentityModel.Tokens;
- using Microsoft.OpenApi.Models;
- using XF.Domain.Cache;
- using XF.Domain.Entities;
- using XF.Domain.Exceptions;
- using XF.Domain.Options;
- using XF.Domain.Repository;
- namespace Hotline.Api
- {
- public static class StartupHelper
- {
- /// <summary>
- /// Authentication
- /// </summary>
- /// <param name="services"></param>
- /// <param name="configuration"></param>
- /// <returns></returns>
- public static IServiceCollection RegisterAuthentication(this IServiceCollection services, ConfigurationManager configuration)
- {
- var jwtOptions = configuration.GetSection("IdentityConfiguration").Get<IdentityConfiguration>().Jwt;
- #region remote ids
- //JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
- //services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
- // .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, d =>
- // {
- // d.Authority = identityConfigs.IdentityUrl;
- // d.RequireHttpsMetadata = false;
- // d.TokenValidationParameters = new TokenValidationParameters
- // {
- // ValidateAudience = false
- // };
- // //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;
- // }
- // };
- // });
- #endregion
- services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
- .AddJwtBearer(d =>
- {
- byte[] bytes = Encoding.UTF8.GetBytes(jwtOptions.SecretKey);
- var secKey = new SymmetricSecurityKey(bytes);
- d.TokenValidationParameters = new()
- {
- ValidateIssuer = false,
- ValidateAudience = true,
- ValidateLifetime = true,
- ValidateIssuerSigningKey = true,
- IssuerSigningKey = secKey,
- AudienceValidator = (audiences, token, parameters) =>
- {
- if (token is JwtSecurityToken jwtToken)
- {
- using var serviceProvider = services.BuildServiceProvider();
- var cacheAudience = serviceProvider.GetService<ITypedCache<AudienceTicket>>();
- var audience = cacheAudience.Get(jwtToken.Subject);
- return audiences != null && audiences.Any(a => a == audience?.Ticket);
- }
- return false;
- }
- };
- //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/hotline")))
- {
- // 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" });
- var files = Directory.GetFiles(AppContext.BaseDirectory).Where(d => d.EndsWith(".xml"));
- foreach (var file in files)
- {
- c.IncludeXmlComments(file, 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>
- /// <param name="configuration"></param>
- /// <param name="corsOrigins"></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;
- config.ForDestinationType<IDataPermission>()
- .Ignore(d => d.CreatorId)
- .Ignore(d => d.CreatorOrgId)
- //.Ignore(d => d.CreatorOrgCode)
- .Ignore(d => d.AreaId);
- config.ForDestinationType<IWorkflow>()
- .Ignore(d => d.ExpiredTimeConfigId);
- config.ForDestinationType<IHasCreationTime>()
- .Ignore(d => d.CreationTime);
- config.ForDestinationType<IHasDeletionTime>().Ignore(d => d.DeletionTime);
- config.ForDestinationType<ISoftDelete>().Ignore(d => d.IsDeleted);
- config.ForDestinationType<IHasModificationTime>().Ignore(d => d.LastModificationTime);
- config.ForDestinationType<Entity>().Ignore(d => d.Id);
- services.AddSingleton(config);
- services.AddScoped<IMapper, ServiceMapper>();
- return services;
- }
- /// <summary>
- /// SignalR
- /// </summary>
- /// <param name="services"></param>
- /// <param name="configuration"></param>
- /// <returns></returns>
- public static IServiceCollection RegisterSignalR(this IServiceCollection services, ConfigurationManager configuration)
- {
- var connectionString = configuration.GetConnectionString("Redis");
- if (string.IsNullOrEmpty(connectionString))
- throw new UserFriendlyException("未配置signalR的redis连接");
- services.AddSignalR().AddStackExchangeRedis(connectionString, options =>
- {
- options.Configuration.ChannelPrefix = "Hotline:signalr:";
- });
- return services;
- }
- public static IServiceCollection RegisterRepository(this IServiceCollection services)
- {
- services.AddScoped(typeof(IRepository<>), typeof(BaseRepository<>));
- services.AddScoped(typeof(IRepositoryWorkflow<>), typeof(BaseRepositoryWorkflow<>));
- services.AddScoped(typeof(IRepositoryTextSearch<>), typeof(BaseRepositoryTextSearch<>));
- return services;
- }
- }
- }
|