public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { var configuration = app.ApplicationServices.GetRequiredService <IConfiguration>(); if (configuration.GetSection("ApiProfilerEnabled").Get <bool>()) { var miniOpts = app.ApplicationServices.GetRequiredService <IOptions <MiniProfilerOptions> >(); var auth = app.ApplicationServices.GetRequiredService <IOptions <ApiProfilerAuthOptions> >(); if (!string.IsNullOrWhiteSpace(miniOpts?.Value?.RouteBasePath) && !string.IsNullOrWhiteSpace(auth?.Value?.Username) && !string.IsNullOrWhiteSpace(auth?.Value?.Password)) { var filterOptions = new BasicAuthAuthorizationFilterOptions { RequireSsl = false, SslRedirect = false, LoginCaseSensitive = true, Users = new[] { new BasicAuthAuthorizationUser { Login = auth?.Value?.Username, PasswordClear = auth?.Value?.Password } } }; var options = new DashboardOptions { Authorization = new[] { new BasicAuthAuthorizationFilter(filterOptions) } }; // 必须在需要控制的中间件之前执行 app.UseWhen(context => context.Request.Path.StartsWithSegments(new PathString(miniOpts.Value.RouteBasePath)), x => x.UseMiddleware <AspNetCoreDashboardMiddleware>(options)); } app.UseCsaApiProfiler(); } }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { //var option = app.ApplicationServices.GetRequiredService<IOptionsMonitor<HangfireOptions>>(); var json = app.ApplicationServices.GetRequiredService <IConfiguration>().GetValue <string>(nameof(HangfireOptions)); var hangfireConfig = JsonConvert.DeserializeObject <HangfireOptions>(json) ?? new HangfireOptions(); var username = hangfireConfig.Username; var password = hangfireConfig.Password; if (hangfireConfig != null && !string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password)) { var hangfireOption = new BasicAuthAuthorizationFilterOptions { RequireSsl = false, SslRedirect = false, LoginCaseSensitive = true, Users = new[] { new BasicAuthAuthorizationUser { Login = username, PasswordClear = password } } }; app.UseHangfireDashboard(options: new DashboardOptions { Authorization = new[] { new BasicAuthAuthorizationFilter(hangfireOption) } }); //app.UseHangfireDashboard("/hangfire", new DashboardOptions //{ // Authorization = new[] { new HangfireAuthorizationFilter() } //}); } else { app.UseHangfireDashboard(); } app.UseHangfireServer(); }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { var options = app.ApplicationServices.GetRequiredService <IOptionsMonitor <HangfireOptions> >(); var config = options.CurrentValue; if (!string.IsNullOrWhiteSpace(config?.Username) && !string.IsNullOrWhiteSpace(config?.Password)) { var hangfireOption = new BasicAuthAuthorizationFilterOptions { RequireSsl = false, SslRedirect = false, LoginCaseSensitive = true, Users = new[] { new BasicAuthAuthorizationUser { Login = config?.Username, PasswordClear = config?.Password } } }; app.UseHangfireDashboard(options: new DashboardOptions { Authorization = new[] { new BasicAuthAuthorizationFilter(hangfireOption) } }); } else { app.UseHangfireDashboard(); } app.UseHangfireServer(); }
/// <summary>Prove o uso do hangfire no serviço</summary> /// <param name="app">Application Builder</param> /// <param name="configuration">Configurações do sistema</param> public static IApplicationBuilder UseHangfireServer(this IApplicationBuilder app, IConfiguration configuration) { var backgroundServerOptions = new BackgroundJobServerOptions(); configuration.Bind("Hangfire:BackgroundJobServerOptions", backgroundServerOptions); var users = configuration .GetSection("Hangfire:DashboardOptions:BasicAuthAuthorizationFilterOptions:Users") .GetChildren() .Where(o => o.GetSection("Login").Exists() && o.GetSection("Password").Exists()) .Select(o => new BasicAuthAuthorizationUser() { Login = o.GetValue <string>("Login"), Password = Convert.FromBase64String(o.GetValue <string>("Password")) }) .ToArray(); var basicAuth = new BasicAuthAuthorizationFilterOptions() { Users = users }; configuration.GetSection("Hangfire:DashboardOptions:BasicAuthAuthorizationFilterOptions").Bind(basicAuth); return(app .UseHangfireServer(backgroundServerOptions) .UseHangfireDashboard( "/hangfire", new DashboardOptions { Authorization = new[] { new BasicAuthAuthorizationFilter(basicAuth) } })); }
public bool Authorize([NotNull] DashboardContext context) { var options = new BasicAuthAuthorizationFilterOptions { RequireSsl = true, LoginCaseSensitive = true, Users = new[] { new BasicAuthAuthorizationUser { Login = _configuration["BasicAuth:Proximity:Key"], PasswordClear = _configuration["BasicAuth:Proximity:Password"] }, new BasicAuthAuthorizationUser { Login = _configuration["BasicAuth:System:Key"], PasswordClear = _configuration["BasicAuth:System:Password"] }, new BasicAuthAuthorizationUser { Login = _configuration["BasicAuth:External:Key"], PasswordClear = _configuration["BasicAuth:External:Password"] } } }; var httpContext = context.GetHttpContext(); var hasAccess = httpContext.User.Identity.IsAuthenticated; if (hasAccess) { var user = new GenericIdentity("Hangfire"); var principal = new GenericPrincipal(user, null); Thread.CurrentPrincipal = principal; } return(hasAccess); }
public static IApplicationBuilder ConfigureSelfHangfire(this IApplicationBuilder app, IConfiguration configuration) { var langStr = configuration.GetSection(HangfireLangKey).Get <string>(); var envLangStr = GetEnvConfig <string>("Lang"); if (!string.IsNullOrEmpty(envLangStr)) { langStr = envLangStr; } if (!string.IsNullOrEmpty(langStr)) { var options = new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture(langStr) }; app.UseRequestLocalization(options); } var services = app.ApplicationServices; var hangfireSettings = services.GetService <IOptions <HangfireSettings> >().Value; var dashbordConfig = new DashboardOptions { AppPath = "#", IgnoreAntiforgeryToken = true, DisplayStorageConnectionString = hangfireSettings.DisplayStorageConnectionString, IsReadOnlyFunc = Context => false }; if (hangfireSettings.HttpAuthInfo.IsOpenLogin && hangfireSettings.HttpAuthInfo.Users.Any()) { var httpAuthInfo = hangfireSettings.HttpAuthInfo; var users = hangfireSettings.HttpAuthInfo.Users.Select(m => new BasicAuthAuthorizationUser { Login = m.Login, Password = m.Password, PasswordClear = m.PasswordClear }); var basicAuthAuthorizationFilterOptions = new BasicAuthAuthorizationFilterOptions { RequireSsl = httpAuthInfo.RequireSsl, SslRedirect = httpAuthInfo.SslRedirect, LoginCaseSensitive = httpAuthInfo.LoginCaseSensitive, Users = users }; dashbordConfig.Authorization = new[] { new BasicAuthAuthorizationFilter(basicAuthAuthorizationFilterOptions) }; } app.UseHangfireDashboard(hangfireSettings.StartUpPath, dashbordConfig); if (!string.IsNullOrEmpty(hangfireSettings.ReadOnlyPath)) { //只读面板,只能读取不能操作 app.UseHangfireDashboard(hangfireSettings.ReadOnlyPath, new DashboardOptions { IgnoreAntiforgeryToken = true, AppPath = hangfireSettings.StartUpPath, //返回时跳转的地址 DisplayStorageConnectionString = false, //是否显示数据库连接信息 IsReadOnlyFunc = Context => true }); } return(app); }
public static IApplicationBuilder ConfigureSelfHangfire(this IApplicationBuilder app, IConfiguration Configuration) { var langStr = Configuration.GetSection(HangfireLangKey).Get <string>(); var envLangStr = GetEnvConfig <string>("Lang"); if (!string.IsNullOrEmpty(envLangStr)) { langStr = envLangStr; } if (!string.IsNullOrEmpty(langStr)) { var options = new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture(langStr) }; app.UseRequestLocalization(options); System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(langStr); } var services = app.ApplicationServices; var hangfireSettings = services.GetService <IOptions <HangfireSettings> >().Value; ConfigFromEnv(hangfireSettings); var queues = hangfireSettings.JobQueues.Select(m => m.ToLower()).Distinct().ToList(); var workerCount = Math.Max(Environment.ProcessorCount, hangfireSettings.WorkerCount); //工作线程数,当前允许的最大线程,默认20 app.UseHangfireServer(new BackgroundJobServerOptions { ServerName = hangfireSettings.ServerName, ServerTimeout = TimeSpan.FromMinutes(4), SchedulePollingInterval = TimeSpan.FromSeconds(15), //秒级任务需要配置短点,一般任务可以配置默认时间,默认15秒 ShutdownTimeout = TimeSpan.FromMinutes(30), //超时时间 Queues = queues.ToArray(), //队列 WorkerCount = workerCount }, additionalProcesses: new[] { new ProcessMonitor() }); var dashbordConfig = new DashboardOptions { AppPath = "#", IgnoreAntiforgeryToken = true, DisplayStorageConnectionString = hangfireSettings.DisplayStorageConnectionString, IsReadOnlyFunc = Context => false }; if (hangfireSettings.HttpAuthInfo.IsOpenLogin && hangfireSettings.HttpAuthInfo.Users.Any()) { var httpAuthInfo = hangfireSettings.HttpAuthInfo; var users = hangfireSettings.HttpAuthInfo.Users.Select(m => new BasicAuthAuthorizationUser { Login = m.Login, Password = m.Password, PasswordClear = m.PasswordClear }); var basicAuthAuthorizationFilterOptions = new BasicAuthAuthorizationFilterOptions { RequireSsl = httpAuthInfo.RequireSsl, SslRedirect = httpAuthInfo.SslRedirect, LoginCaseSensitive = httpAuthInfo.LoginCaseSensitive, Users = users }; dashbordConfig.Authorization = new[] { new BasicAuthAuthorizationFilter(basicAuthAuthorizationFilterOptions) }; } app.UseHangfireDashboard(hangfireSettings.StartUpPath, dashbordConfig); if (!string.IsNullOrEmpty(hangfireSettings.ReadOnlyPath)) { //只读面板,只能读取不能操作 app.UseHangfireDashboard(hangfireSettings.ReadOnlyPath, new DashboardOptions { IgnoreAntiforgeryToken = true, AppPath = hangfireSettings.StartUpPath, //返回时跳转的地址 DisplayStorageConnectionString = false, //是否显示数据库连接信息 IsReadOnlyFunc = Context => true }); } return(app); }
public DashboardAuthorizationFilter(BasicAuthAuthorizationFilterOptions options) : base(options) { this.options = options ?? throw new ArgumentNullException(nameof(options)); }
public static void ConfigureHangfire(this IApplicationBuilder app) { var services = app.ApplicationServices; var hangfireSettings = services.GetService <IOptions <HangfireSettings> >().Value; var queues = hangfireSettings.JobQueues.Select(m => m.ToUpper()).Distinct().ToList(); if (!queues.Contains("DEFAULT")) { queues.Add("DEFAULT"); } var workerCount = Math.Max(Environment.ProcessorCount, hangfireSettings.WorkerCount); //工作线程数,当前允许的最大线程,默认20 app.UseHangfireServer(new BackgroundJobServerOptions { ServerTimeout = TimeSpan.FromMinutes(4), SchedulePollingInterval = TimeSpan.FromSeconds(15), //秒级任务需要配置短点,一般任务可以配置默认时间,默认15秒 ShutdownTimeout = TimeSpan.FromMinutes(30), //超时时间 Queues = queues.ToArray(), //队列 WorkerCount = workerCount }); var dashbordConfig = new DashboardOptions { AppPath = "#", DisplayStorageConnectionString = hangfireSettings.DisplayStorageConnectionString, IsReadOnlyFunc = Context => false }; if (hangfireSettings.HttpAuthInfo.IsOpenLogin && hangfireSettings.HttpAuthInfo.Users.Any()) { var httpAuthInfo = hangfireSettings.HttpAuthInfo; var users = hangfireSettings.HttpAuthInfo.Users.Select(m => new BasicAuthAuthorizationUser { Login = m.Login, Password = m.Password, PasswordClear = m.PasswordClear }); var basicAuthAuthorizationFilterOptions = new BasicAuthAuthorizationFilterOptions { RequireSsl = httpAuthInfo.RequireSsl, SslRedirect = httpAuthInfo.SslRedirect, LoginCaseSensitive = httpAuthInfo.LoginCaseSensitive, Users = users }; dashbordConfig.Authorization = new[] { new BasicAuthAuthorizationFilter(basicAuthAuthorizationFilterOptions) }; } app.UseHangfireDashboard(hangfireSettings.StartUpPath, dashbordConfig); if (!string.IsNullOrEmpty(hangfireSettings.ReadOnlyPath)) { //只读面板,只能读取不能操作 app.UseHangfireDashboard(hangfireSettings.ReadOnlyPath, new DashboardOptions { IgnoreAntiforgeryToken = true, AppPath = hangfireSettings.StartUpPath, //返回时跳转的地址 DisplayStorageConnectionString = false, //是否显示数据库连接信息 IsReadOnlyFunc = Context => true }); } }