如何配置一个用户具有 ReadOnly 访问权限而另一个用户具有对 Hangfire 仪表板的完全访问权限?

How can I config one user has ReadOnly access and another user have full access to Hangfire Dashboard?

如何配置一个用户具有只读访问权限而另一个用户具有对仪表板的完全访问权限?

您可以使用 DashboardOptions 和 AuthorizationFilter 设置只读和编辑权限。 See Documentation from Hangfire

public class HangFireAuthorizationFilter : IDashboardAuthorizationFilter
{
    public bool Authorize([NotNull] DashboardContext context)
    {
        string user = HttpContext.Current.User.Identity.Name;
        var adminAuthz = InternalMethod.lookup_db_for_user_access(user, "View");

        return adminAuthz != null;
    }

    public bool IsUserAuthorizedToEditHangfireDashboard([NotNull] DashboardContext context)
    {
        string user = HttpContext.Current.User.Identity.Name;
        var adminAuthz = InternalMethod.lookup_db_for_user_access(user, "Edit");

        return adminAuthz != null;
    }
}

在你的 Hangfire 仪表板初始化中使用上面的过滤器

public void Configuration(IAppBuilder app)
{
    var hangfireAuthz = new HangFireAuthorizationFilter();
    var dashboardOptions = new DashboardOptions
    {
        Authorization = new[] { hangfireAuthz },
        IsReadOnlyFunc = (DashboardContext context) => !hangfireAuthz.IsUserAuthorizedToEditHangfireDashboard(context)
    };

    app.UseHangfireDashboard("/hangfire", dashboardOptions);
}