Hangfire dashboard authorization in Azure WorkerRole OR Self Hosted application

99封情书 提交于 2019-12-11 11:48:23

问题


It is very recent that I have introduced to Hangfire, and I must say it is awesome.

I am working on an application, wherein I am hosting hangfire in Azure worker role. Everything works perfect; the hangfire configuration, job scheduling, dashboard etc. except configuring authorization to hangfire dashboard.

I have added an OwinStartup class where I have configuring hangfire dashboard. I have used my custom implementation of IAuthorizationFilter and an OwinMiddleware, anticipating that user should now be prompted to provide credentials while accessing hangfire dashboard. But for no help, and it keep giving me 403 response when trying to access the dashboard. :(

It works perfectly alright if I don't use authorization filter option while configuring dashboard, but then everyone could access it.

This is my Startup class -

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

        app.Use(typeof(AuthenticationMiddleware));

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

I have written OWIN middleware i.e. AuthenticationMiddleware as suggested here

...and my custom 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;
     }
}

This is how I am configuring dashboard in OnStart method of my worker role. (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));

I guess the solution for hangfire dashboard in self hosted applicatinon should work as well


回答1:


The following nuget package come to rescue for basic authentication -

Thinktecture.IdentityModel.Owin.BasicAuthentication

The package is available here - https://www.nuget.org/packages/Thinktecture.IdentityModel.Owin.BasicAuthentication/)

Get this package and simply call the following in your owin startup class, instead of your custom middlewawre -

app.UseBasicAuthentication("SomeName", ValidateUser);

...where ValidateUser is the function to validate the user.

    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);
    }

And your are done! Now when you will access hangfire dashboard, you will be prompted for credentials.



来源:https://stackoverflow.com/questions/34767082/hangfire-dashboard-authorization-in-azure-workerrole-or-self-hosted-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!