【发布时间】:2018-06-19 06:11:41
【问题描述】:
使用 hangfire 版本:1.6.17
我已在 aspnetcore 2.0
上成功设置 hangifire
我通过使用添加了授权:
app.UseHangfireDashboard("/jobs", new DashboardOptions
{
Authorization = new[] { new HangfireAuthorizationFilter() }
});
和
public class HangfireAuthorizationFilter :IDashboardAuthorizationFilter
{
private const string PERMISSION = "read:jobs";
public bool Authorize(DashboardContext context)
{
var httpContext = context.GetHttpContext();
// allow only users with correct permission
if (httpContext.User.Identity.IsAuthenticated)
{
var permissions = httpContext.User.Claims.FirstOrDefault(x => x.Type.Equals(CustomClaims.Permissions))?.Value?.Split(' ');
return permissions?.Contains(PERMISSION) ?? false;
}
return false;
}
}
我无法解决的唯一问题是向用户返回了一个带有 401 的空白屏幕,而不是默认的挑战 /account/login。
如果您使用 [Authorize] 属性访问我的控制器,它们会自动重定向到 /account/login,因此登录路径正常工作。
即使我特别指定,用户在未经授权访问 Hangfire 时也不会被重定向:
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.LoginPath = "/Account/Login/";
})
有人提出想法,或者我应该将其标记为 Hangfire github 上的错误。
【问题讨论】:
-
您好,您找到解决方法了吗?
-
不,但我通过重定向到登录页面并在查询字符串中自己指定重定向 URL 来解决问题。不是一个体面的工作,但没有其他选择,因为我没有得到任何反馈。
标签: asp.net-core authorization openid-connect hangfire