Azure WorkerRole 或自托管应用程序中的 Hangfire 仪表板授权
Hangfire dashboard authorization in Azure WorkerRole OR Self Hosted application
Hangfire 是我最近才接触到的,不得不说它很棒。
我正在开发一个应用程序,其中我以 Azure 工作者角色托管 hangfire。 一切正常; hangfire 配置、作业调度、仪表板等。除了配置对 hangfire 仪表板的授权。
我添加了一个 OwinStartup
class 用于配置 hangfire 仪表板。我使用了 IAuthorizationFilter
和 OwinMiddleware
的自定义实现,预计现在应该提示用户在访问 hangfire 仪表板时提供凭据。但没有任何帮助,它在尝试访问仪表板时一直给我 403
响应。 :(
如果我在配置仪表板时不使用授权过滤器选项,它工作得很好,但每个人都可以访问它。
这是我的初创公司 class -
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));
我想自托管应用程序中 hangfire 仪表板的解决方案也应该有效
以下 nuget 包可用于基本身份验证 -
Thinktecture.IdentityModel.Owin.BasicAuthentication
包在这里可用 -
https://www.nuget.org/packages/Thinktecture.IdentityModel.Owin.BasicAuthentication/)
获取此包并在您的 owin 启动 class 中调用以下命令,而不是您的自定义中间件 -
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 仪表板时,系统会提示您输入凭据。
Hangfire 是我最近才接触到的,不得不说它很棒。
我正在开发一个应用程序,其中我以 Azure 工作者角色托管 hangfire。 一切正常; hangfire 配置、作业调度、仪表板等。除了配置对 hangfire 仪表板的授权。
我添加了一个 OwinStartup
class 用于配置 hangfire 仪表板。我使用了 IAuthorizationFilter
和 OwinMiddleware
的自定义实现,预计现在应该提示用户在访问 hangfire 仪表板时提供凭据。但没有任何帮助,它在尝试访问仪表板时一直给我 403
响应。 :(
如果我在配置仪表板时不使用授权过滤器选项,它工作得很好,但每个人都可以访问它。
这是我的初创公司 class -
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));
我想自托管应用程序中 hangfire 仪表板的解决方案也应该有效
以下 nuget 包可用于基本身份验证 -
Thinktecture.IdentityModel.Owin.BasicAuthentication
包在这里可用 - https://www.nuget.org/packages/Thinktecture.IdentityModel.Owin.BasicAuthentication/)
获取此包并在您的 owin 启动 class 中调用以下命令,而不是您的自定义中间件 -
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 仪表板时,系统会提示您输入凭据。