/// <summary> /// 使用约定激活的中间件,注入的对象无法通过构造函数获取,只能在Invoke里获取 /// </summary> /// <param name="context"></param> /// <param name="next"></param> /// <param name="oneZeroContext"></param> /// <returns></returns> public async Task Invoke(HttpContext context, OneZeroContext oneZeroContext, OneZeroOption oneZeroOption) { _oneZeroContext = oneZeroContext; _oneZeroContext.IsAuththentic = oneZeroOption.IsAuthentic.CastTo(false); _oneZeroContext.RequestIP = context.Connection.RemoteIpAddress.ToString(); _oneZeroContext.ActionPath = context.Request.Path; //是否开启身份验证 if (_oneZeroContext.IsAuththentic) { _oneZeroContext.TokenIP = context.User.HasClaim(v => v.Type == "ip") ? context.User.Claims.Where(v => v.Type == "ip").Select(v => v.Value).First() : ""; _oneZeroContext.UserId = context.User.HasClaim(v => v.Type == "userid") ? context.User.Claims.Where(v => v.Type == "userid").Select(v => v.Value).First().CastTo(default(Guid)) : default(Guid); _oneZeroContext.TenanId = context.User.HasClaim(v => v.Type == "tenanid") ? context.User.Claims.Where(v => v.Type == "tenanid").Select(v => v.Value).First().CastTo(default(Guid)) : oneZeroOption.DefaultTenanId.ConvertToGuid("TenanId配置"); _oneZeroContext.MenuList = context.User.Claims.Where(v => v.Type == "menus")?.Select(v => v.Value); _oneZeroContext.RoleList = context.User.Claims.Where(v => v.Type == "roles")?.Select(v => v.Value); _oneZeroContext.PermissionList = context.User.Claims.Where(v => v.Type == "permissions")?.Select(v => v.Value); //通过token携带信息判断,该请求是否合法 TokenValidate(context, oneZeroContext); } else { //默认租户ID _oneZeroContext.TenanId = oneZeroOption.DefaultTenanId.ConvertToGuid("TenanId配置"); } await _next(context); }
public QiniuHelper(OneZeroOption option) { AccessKey = option.QiniuOption.AccessKey; SecretKey = option.QiniuOption.SecretKey; Bucket = option.QiniuOption.Bucket; Domain = option.QiniuOption.Domain; ExpireSeconds = Convert.ToInt32(option.QiniuOption.ExpireSeconds); }
/// <summary> /// 配置框架服务 /// </summary> /// <param name="services"></param> /// <param name="configuration"></param> /// <param name="oneZeroOption"></param> /// <returns></returns> public static IServiceCollection AddOneZero(this IServiceCollection services, IConfiguration configuration, OneZeroOption oneZeroOption) { //获取所有程序集 var assemblies = GetAssemblies(); services.AddMediatR(assemblies); services.AddAutoMapper(assemblies); services.AddHttpContextAccessor(); services.AddScoped <OneZeroContext>(); services.AddScoped <OneZeroExceptionMiddleware>(); services.AddScoped <OneZeroMiddleware>(); services.AddScoped <QiniuHelper>(); services.AddScoped <JwtService>(); services.AddScoped <IDapperProvider, DapperProvider>(); services.AddSingleton(oneZeroOption); //添加日志面板 //services.AddLogDashboard(option => { option.CustomLogModel<MyLogModel>(); }); //根据配置决定是否使用Swagger,Hangfire services.ConfigSwagger(configuration, oneZeroOption.IsAuthentic.CastTo(false)) .ConfigHangfire(configuration, oneZeroOption) .ConfigAuthentication(oneZeroOption); //添加跨域 services.AddCors(options => { options.AddPolicy("AllowCrossOrigin", b => { b.AllowAnyHeader() .AllowAnyMethod() .AllowAnyOrigin(); }); }); return(services); }
public static IServiceCollection ConfigAuthentication(this IServiceCollection services, OneZeroOption oneZeroOption) { services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(o => { o.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters { ValidIssuer = oneZeroOption.JwtOption.Issuer, ValidAudience = oneZeroOption.JwtOption.Audience, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(oneZeroOption.JwtOption.SecretKey)), //时间偏移量 ClockSkew = new TimeSpan(0, 0, 30), RequireExpirationTime = true, ValidateLifetime = true }; }); return(services); }
/// <summary> /// 配置Hangfire /// </summary> /// <param name="services"></param> /// <param name="configuration"></param> /// <returns></returns> public static IServiceCollection ConfigHangfire(this IServiceCollection services, IConfiguration configuration, OneZeroOption oneZeroOption) { if (configuration["OneZero:Hangfire:IsUse"].CastTo(false)) { if (configuration["OneZero:Hangfire:Storage"] == "redis") { if (string.IsNullOrWhiteSpace(oneZeroOption.DefaulRedisConnectionString)) { throw new OneZeroException("Hangfire连接字符串配置有误,请检查配置", ResponseCode.Fatal); } services.AddHangfire(x => x.UseRedisStorage(oneZeroOption.DefaulRedisConnectionString)); } else { if (string.IsNullOrWhiteSpace(oneZeroOption.DefaultConnectionString)) { throw new OneZeroException("Hangfire连接字符串配置有误,请检查配置", ResponseCode.Fatal); } services.AddHangfire(x => x.UseSqlServerStorage(oneZeroOption.DefaultConnectionString)); } } return(services); }
public JwtService(OneZeroOption oneZeroOption) { _oneZeroOption = oneZeroOption; }
/// <summary> /// 配置数据库上下文 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="services"></param> /// <param name="option"></param> /// <param name="loggerFactory"></param> /// <param name="dbConnection"></param> /// <param name="assmblyName"></param> /// <param name="Dbtype"></param> /// <param name="poolSize"></param> /// <returns></returns> public static IServiceCollection ConfigDbContext <T>(this IServiceCollection services, OneZeroOption option, ILoggerFactory loggerFactory, string dbConnection, string assmblyName = null, DatabaseType Dbtype = DatabaseType.SqlServer, int poolSize = 128) where T : DbContext { //services.AddDbContextPool<T>(options => { options.GetDbCOntextOptionByDatabaseType(Dbtype, loggerFactory, dbConnection, assmblyName); }, poolSize); services.AddDbContextPool <T>(options => { options.UseSqlServer(dbConnection, b => b.MigrationsAssembly(assmblyName)); options.UseLoggerFactory(loggerFactory); }, poolSize); return(services); }