2016-01-13 20 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儀表板時,系統會提示您輸入憑據。