【问题标题】:FluentSecurity: RequireRolePolicyViolationHandler does not get calledFluentSecurity:RequireRolePolicyViolationHandler 不会被调用
【发布时间】:2013-07-09 09:34:00
【问题描述】:

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

在我的设置中,我有

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

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

我错过了什么吗?根据 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。如果您转到 /Logged,您将被重定向到登录页面,因此 DenyAnonymousAccessPolicyViolationHandler 可以工作。您可以使用任何您想要的用户名和密码登录。转到Settings,您会看到您被重定向到登录页面,而不是执行RequireRolePolicyViolationHandler

【问题讨论】:

  • 您能否发布一些代码来展示您是如何设置的,以便我们尝试帮助您。
  • @lopezbertoni 我的设置和链接的文章完全一样
  • 请发布您的 DenyAnonymousAccessPolicyViolationHandler 代码。这可能只是重定向到登录页面。另外,发布您如何配置 Fluent Security、如何获得角色等。不看代码很难提供帮助。
  • @lopezbertoni 我编辑了我的问题。

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


【解决方案1】:

这是我的设置方法,希望对您有所帮助:

在 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 Security 的有效解决方案。我发布了我认为是核心配置的内容。希望这可以帮助。

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

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。

【讨论】:

  • 我认为唯一的区别是我们如何为用户获取角色。这可能是问题吗? Ninject 的东西是等价的
  • @IgorKulman 我更新了以显示我实际上是如何获得这些角色的。我很难判断这是否是问题所在,但欢迎您尝试一下。
  • 尝试停止 var roles = Roles.Providers["MemberAccountRoleProvider"];看看它是否正在执行。如果根本没有调用它,则可能存在配置问题。
  • FluentSecurity 根据调用堆栈尝试访问被禁止的控制器时确实会执行
  • @IgorKulman 更新了答案,您提供的代码运行良好。
猜你喜欢
  • 2020-01-26
  • 2020-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-01
  • 2019-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多