2016-01-13 57 views
1

我已经向Hangfire介绍了最近的内容,我必须说它非常棒。Azure WorkerRole或自托管应用程序中的Hangfire仪表板授权

我正在研究一个应用程序,其中我在Azure工作者角色中进行了hangfire。 一切正常完美;除了将授权配置为hangfire仪表板以外,它还包括hangfire配置,作业调度,仪表板等

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

如果在配置的仪表板,我不使用授权筛选选项,但当时每个人都可以访问它它的工作原理完全没有问题

这是我的启动类 -

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

     app.Use(typeof(AuthenticationMiddleware)); 

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

我已经写OWIN中间件即AuthenticationMiddleware的建议here

...和我的自定义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)); 

我想自托管applicatinon为迟发型仪表板解决方案应该工作以及

回答

1

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

Thinktecture.IdentityModel .Owin.BasicAuthentication

该软件包在这里可用 - https://www.nuget.org/packages/Thinktecture.IdentityModel.Owin.BasicAuthentication/

得到这个包,只需拨打您的owin启动级以下,而不是您的自定义middlewawre -

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仪表板时,系统会提示您输入凭据。