public static IApplicationBuilder UseAuthentication(this IApplicationBuilder applicationBuilder, SchedulerOptions schedulerOptions) { applicationBuilder.Use(async(context, next) => { var request = context.Request; var header = request.Headers["Authorization"]; if (!string.IsNullOrWhiteSpace(header)) { var authHeader = System.Net.Http.Headers.AuthenticationHeaderValue.Parse(header); if ("Basic".Equals(authHeader.Scheme, StringComparison.OrdinalIgnoreCase)) { var parameter = Encoding.UTF8.GetString(Convert.FromBase64String(authHeader.Parameter ?? "")); var(suppliedUserName, suppliedPassword) = parameter.GetCredentials(); var(userName, password) = schedulerOptions.AdminCredentials.GetCredentials(); if (suppliedUserName.Equals(userName, StringComparison.OrdinalIgnoreCase) && suppliedPassword == password) { await next(); return; } } } context.Response.StatusCode = 401; context.Response.Headers["WWW-Authenticate"] = "Basic realm=\"App\""; }); return(applicationBuilder); }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.Configure <KestrelServerOptions>(options => { options.AllowSynchronousIO = true; }); services.AddSingleton <SchedulePublisher>(); var schedulerOptions = new SchedulerOptions(); Configuration.GetSection(SchedulerOptions.ConfigurationSection).Bind(schedulerOptions); services.AddSingleton(schedulerOptions); var connectionString = Configuration.GetConnectionString("SchedulerDb"); services.AddQuartz(q => { q.SchedulerId = "a"; q.UseDefaultThreadPool(tp => { tp.MaxConcurrency = schedulerOptions.MaxConcurrency; }); q.UseMicrosoftDependencyInjectionJobFactory(options => { options.CreateScope = false; }); q.UsePersistentStore(s => { s.UseProperties = true; s.RetryInterval = TimeSpan.FromSeconds(15); switch (schedulerOptions.DatabaseType) { case SchedulerOptions.DatabaseTypeSqlite: s.UseSQLite(sqlite => { sqlite.ConnectionString = connectionString; }); break; case SchedulerOptions.DatabaseTypeMsSql: s.UseSqlServer(sqlServer => { sqlServer.ConnectionString = connectionString; }); break; case SchedulerOptions.DatabaseTypePgSql: s.UsePostgres(postgres => { postgres.ConnectionString = connectionString; }); break; case SchedulerOptions.DatabaseTypeMySql: s.UseMySql(mysql => { mysql.ConnectionString = connectionString; }); break; default: throw new Exception("Invalid database type in configuration"); } s.UseJsonSerializer(); }); }); services.AddQuartzServer(options => { // when shutting down we want jobs to complete gracefully options.WaitForJobsToComplete = true; }); var identity = new GenericIdentity("scheduler"); var principal = new ClaimsPrincipal(identity); var requestContext = new RequestContext(); requestContext.Set(principal); services.AddSingleton(requestContext); services.AddBus(); services.AddBusPublish(); services.AddBusConsume(); services.AddHealthChecks(); }