【问题标题】:Hangfire dashboard authorization in Azure WorkerRole OR Self Hosted applicationAzure WorkerRole 或自托管应用程序中的 Hangfire 仪表板授权
【发布时间】:2016-01-13 12:44:14
【问题描述】:

这是我最近才介绍给 Hangfire 的,我必须说它很棒。

我正在开发一个应用程序,其中我以 Azure 工作者角色托管 hangfire。 一切都很完美; hangfire 配置、作业调度、仪表板等。除了配置hangfire 仪表板的授权

我添加了一个OwinStartup 类,我在其中配置了hangfire 仪表板。我使用了IAuthorizationFilterOwinMiddleware 的自定义实现,预计现在应该提示用户在访问hangfire 仪表板时提供凭据。但没有任何帮助,它在尝试访问仪表板时不断给我403 响应。 :(

如果我在配置仪表板时不使用授权过滤器选项,它工作得很好,但是每个人都可以访问它。

这是我的 Startup 课程 -

    public void Configuration(IAppBuilder app)
    {
        app.UseWelcomePage("/");

        app.Use(typeof(AuthenticationMiddleware));

        app.UseHangfireDashboard("/hangfire", new DashboardOptions
        {
            AuthorizationFilters = new[] { new MyAuthorization() }
        });
    }

我已经按照here的建议编写了 OWIN 中间件,即AuthenticationMiddleware

...还有我的自定义IAuthorizationFilter

public class MyAuthorization : IAuthorizationFilter
{
     public bool Authorize(IDictionary<string, object> owinEnvironment)
     {
         var context = new OwinContext(owinEnvironment);

         // Allow all authenticated users to see the Dashboard 
         return context.Authentication.User.Identity.IsAuthenticated;
     }
}

这就是我在工作角色的OnStart 方法中配置仪表板的方式。 (ref)

var endpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["WorkerRoleEndpoint"];
string baseUri = String.Format("{0}://{1}", endpoint.Protocol, endpoint.IPEndpoint);//http://127.0.0.1:81/hangfire

owinApp = WebApp.Start<HangfireDashboardStartup>(new StartOptions(url: baseUri));

我猜自托管应用程序中的hangfire仪表板解决方案应该也可以工作

【问题讨论】:

    标签: c# azure-worker-roles hangfire


    【解决方案1】:

    以下 nuget 包来拯救基本身份验证 -

    Thinktecture.IdentityModel.Owin.BasicAuthentication

    该软件包可在此处获得 - https://www.nuget.org/packages/Thinktecture.IdentityModel.Owin.BasicAuthentication/)

    获取此包并在您的 owin startup 类中调用以下内容,而不是您的自定义中间件 -

    app.UseBasicAuthentication("SomeName", ValidateUser);
    

    ...ValidateUser 是验证用户的函数。

        private Task<IEnumerable<Claim>> ValidateUser(string id, string secret)
        {
            if (id == secret) //Dummy validation, modify it accordingly
            {
                var claims = new List<Claim>
                {
                    new Claim(ClaimTypes.NameIdentifier, id),
                    new Claim(ClaimTypes.Role, "Foo")
                };
                return Task.FromResult<IEnumerable<Claim>>(claims);
            }
            return Task.FromResult<IEnumerable<Claim>>(null);
        }
    

    你已经完成了!现在,当您访问 hangfire 仪表板时,系统会提示您输入凭据。

    【讨论】:

      猜你喜欢
      • 2023-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多