我的ASP.NET
项目使用登录屏幕,允许用户输入凭据以登录到Web应用程序。此时,将在会话对象中设置用户对象。User 对象包含有关登录用户角色的信息。现在,我希望hangfire
仪表板是用户登录后可见的应用程序内的链接。单击链接后,根据用户的角色是否合适,我想显示或限制仪表板。我该怎么做?我无法访问 OWIN startup.cs
类中的会话对象。
您可以使用定义
如下的 IAuthorizationFilter 来限制对仪表板的访问:
public class DashboardAuthorizationFilter : IAuthorizationFilter
{
public bool Authorize(IDictionary<string, object> owinEnvironment)
{
// In case you need an OWIN context, use the next line,
// `OwinContext` class is the part of the `Microsoft.Owin` package.
//var context = new OwinContext(owinEnvironment);
var ok = false;
if (HttpContext.Current != null && HttpContext.Current.User != null)
{
ok = IsAuthorizedForDashboard(HttpContext.Current.User);
}
return ok;
}
}
其中IsAuthorizedForDashboard
是您需要创建的函数。
然后像这样注册过滤器:
app.UseHangfireDashboard(DashboardPath,
new DashboardOptions {
AuthorizationFilters = new List<IAuthorizationFilter> {
new DashboardAuthorizationFilter() }
});