FluentSecurity:不会调用RequireRolePolicyViolationHandler

时间:2013-07-09 09:34:01

标签: c# asp.net asp.net-mvc security fluent-security

我有一个带有FluentSecurity的ASP.NET MVC页面。我根据this article使用Ninject进行设置。我有DenyAnonymousAccessPolicyViolationHandler效果很好。我添加了RequireRolePolicyViolationHandler

在我的设置中,我有

configuration.For<SettingsController>().RequireRole(CMSRoles.Admin);

如果我使用没有所需角色的用户导航到SettingsController,则不会调用RequireRolePolicyViolationHandler。相反,我被重定向到web.config中定义的LogOn页面。

我错过了什么吗?根据FluentSecurity文档,它应该可以工作。

编辑:我已经注册了自定义RoleProvider,并将其与FluentSecurity一起使用:

configuration.GetAuthenticationStatusFrom(() => HttpContext.Current.User.Identity.IsAuthenticated);
configuration.GetRolesFrom(() => Roles.GetRolesForUser(HttpContext.Current.User.Identity.Name));

编辑:我创建了一个最小样本应用:https://dl.dropboxusercontent.com/u/73642/MvcApplication1.zip。如果您转到/已记录,您将被重定向到登录页面,以便DenyAnonymousAccessPolicyViolationHandler正常工作。您可以使用您想要的任何用户名和密码登录。转到Settings,您会看到您被重定向到登录页面而不是执行RequireRolePolicyViolationHandler

1 个答案:

答案 0 :(得分:0)

以下是我设置的方法,希望这会有所帮助:

在App_Start / NinjectWebCommon.cs中,我绑定了策略处理程序:

kernel.Bind<IPolicyViolationHandler>().To<DenyAnonymousAccessPolicyViolationHandler>();
kernel.Bind<IPolicyViolationHandler>().To<RequireRolePolicyViolationHandler>();

我也像这样配置Fluent Security(使用Ninject服务定位器):

var locator = new NinjectServiceLocator(kernel);
ServiceLocator.SetLocatorProvider(() => locator);

SecurityConfigurator.Configure(
            configuration =>
            {
                configuration.GetAuthenticationStatusFrom(() => HttpContext.Current.User.Identity.IsAuthenticated);
                configuration.GetRolesFrom(SecurityHelpers.UserRoles);

                //HomeController and other configurations
                configuration.For<HomeController>().Ignore();

                configuration.ResolveServicesUsing(ServiceLocator.Current.GetAllInstances);
             }
             );
GlobalFilters.Filters.Add(new HandleSecurityAttribute(), 0);

然后对于每个策略,我都有一个IPolicyViolationHandler

的实现
public class RequireRolePolicyViolationHandler : IPolicyViolationHandler
{
    public ActionResult Handle(PolicyViolationException exception)
    {
        //Make sure you're redirecting to the desired page here. You should put a stop here to debug it and see if it's being hit. 
        return new HttpUnauthorizedResult(exception.Message);
    }
}

我有一个使用自定义成员资格/角色提供程序和Fluent安全性的工作解决方案。我发布了我认为的核心配置。希望这可以帮助。

编辑:添加了如何获取角色。

public static class SecurityHelpers
{
    public static IEnumerable<object> UserRoles()
    {
        var currentUser = HttpContext.Current.User.Identity.Name;
        var roles = Roles.Providers["MemberAccountRoleProvider"]; //Custom Role Provider Name
        return currentUser != null ? roles.GetRolesForUser(currentUser).Cast<object>().ToArray() : null;

    }
}

编辑2 : 我看了你的代码,它工作正常。将其添加到您的代码中,以便您可以重定向到您想要的位置。现在你只是返回一个Http结果:

public class RequireRolePolicyViolationHandler : IPolicyViolationHandler
{
    public ActionResult Handle(PolicyViolationException exception)
    {
        //return new HttpUnauthorizedResult(exception.Message);
        return
            new RedirectToRouteResult(
                new RouteValueDictionary(new { action = "Test", controller = "Account"})); //Created a view for testing
    }
}

当我尝试获取设置页面时,我正在访问RequireRolePolicyViolationHandler。 Settings Page Debugger

相关问题