关于 hangfire 的权限问题

hangfire 是一个分布式后台执行服务。

官网:http://hangfire.io/

 

我看中hangfire的地方是

1:使用简单

2:多种持久化保存方案。支持sqlserver ,msmq等 ,其他的redis 等持久化方案要收费。不过自己扩展不是难事。hangfire基于net3.5的extension扩展。

3:有监控系统,并且可以和其他监控系统集成。 

 

回顾正题:

hangfire在部署到iis环境上,通过地址访问的时候会出现401未授权错误。通过代码分析是由于hangfire内建授权机制造成的问题。

在分析源码前,建议先对owin做个了解:

http://www.cnblogs.com/dudu/p/what-is-owin.html

http://owin.org/

hangfire继承了OwinMiddleware,在每次请求的时候会去执行IAuthorizationFilter的实现。

   internal class DashboardMiddleware : OwinMiddleware
    {
        private readonly JobStorage _storage;
        private readonly RouteCollection _routes;
        private readonly IEnumerable<IAuthorizationFilter> _authorizationFilters;

     
        public override Task Invoke(IOwinContext context)
        {
            var dispatcher = _routes.FindDispatcher(context.Request.Path.Value);
            
            if (dispatcher == null)
            {
                return Next.Invoke(context);
            }
             
            foreach (var filter in _authorizationFilters)
            {
                if (!filter.Authorize(context.Environment))
                {
                    context.Response.StatusCode = (int) HttpStatusCode.Unauthorized;
                    return Task.FromResult(false);
                }
            }

            var dispatcherContext = new RequestDispatcherContext(
                _storage,
                context.Environment,
                dispatcher.Item2);

            return dispatcher.Item1.Dispatch(dispatcherContext);
        }
    }

 

hangfire默认加载了 LocalRequestsOnlyAuthorizationFilter 

    public class LocalRequestsOnlyAuthorizationFilter : IAuthorizationFilter
    {
        public bool Authorize(IDictionary<string, object> owinEnvironment)
        {
            var context = new OwinContext(owinEnvironment);
            var remoteAddress = context.Request.RemoteIpAddress;

            // if unknown, assume not local
            if (String.IsNullOrEmpty(remoteAddress))
                return false;

            // check if localhost
            if (remoteAddress == "127.0.0.1" || remoteAddress == "::1")
                return true;

            // compare with local address
            if (remoteAddress == context.Request.LocalIpAddress)
                return true;

            return false;
        }
    }

可以看出来对remoteaddress做了限制。

 

如果不考虑安全的场合,可以采用以下做法:

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseHangfire(config =>
            {
                config.UseAuthorizationFilters(new DontUseThisAuthorizationFilter());

                config
                    .UseSqlServerStorage(@"server=xxxxx;database=Hangfire;uid=sa;pwd=123.com")
                    .UseMsmqQueues(@".\Private$\hangfire{0}", "default", "critical");
            });

             app.MapHangfireDashboard();
        }
    }

 

    public class DontUseThisAuthorizationFilter : IAuthorizationFilter
    {
        public bool Authorize(IDictionary<string, object> owinEnvironment)
        {
            return true;
        }
    }

 

 

 

如果需要结合现有系统权限机制的场合,也是实现IAuthorizationFilter:

 GlobalContext.Current.UserInfo是我们系统内部的一个上下文class。
    public class CustomAuthorizationFilter : IAuthorizationFilter
    {
        public bool Authorize(IDictionary<string, object> owinEnvironment)
        {
            var context = new OwinContext(owinEnvironment);
            if ( GlobalContext.Current.UserInfo==null){
                string urls = "/Index/Login?url=" + context.Request.Uri.Host;
                context.Response.Redirect(urls);
                return false;  
            }
            return true; 
        }
    }        

 

转载于:https://www.cnblogs.com/yuanhuaming/p/4378290.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hangfire 是一个用于 .NET 平台的开源后台作业管理框架,它允许您在 ASP.NET 应用程序中轻松执行后台作业,如发送电子邮件、生成报告、清理数据库等。 Hangfire 提供了一种简单、直观的方式来定义和执行后台作业。您可以使用 Hangfire 在后台执行方法、定时任务、队列任务等。它还提供了一个基于 WEB 的用户界面,以便您可以轻松地查看、管理和监控您的后台作业。 以下是 Hangfire 的一些主要特点: 1. 随时随地启动、停止或暂停后台任务。 2. 支持定时任务、延迟任务和队列任务。 3. 支持多种存储方式,如 SQL Server、PostgreSQL、Redis 等。 4. 提供了一个 WEB 界面,方便您查看后台任务的执行情况。 5. 支持分布式部署,允许多个应用程序使用同一个任务队列。 下面是一个简单的示例代码,演示了如何使用 Hangfire 在后台执行一个方法: ``` public class MyJob { public void DoJob() { // 执行后台任务 Console.WriteLine("Hello, Hangfire!"); } } // 在 Startup.cs 中配置 Hangfire public class Startup { public void Configuration(IAppBuilder app) { // 配置 Hangfire GlobalConfiguration.Configuration.UseSqlServerStorage("connectionString"); app.UseHangfireDashboard(); app.UseHangfireServer(); // 注册后台作业 var job = new MyJob(); BackgroundJob.Enqueue(() => job.DoJob()); } } ``` 在这个示例中,我们首先定义了一个名为 MyJob 的类,其中包含了一个名为 DoJob 的方法。然后,在 Startup.cs 中配置 Hangfire,指定了使用 SQL Server 存储后台任务,并启用了 Hangfire 的 Dashboard 和 Server。最后,我们使用 BackgroundJob.Enqueue 方法注册了一个后台任务,使其在启动应用程序后立即执行。 使用 Hangfire,您可以轻松地实现后台任务管理,并提高应用程序的可靠性和可扩展性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值