【问题标题】:How to redirect to specific page in FluentSecurity?如何重定向到 FluentSecurity 中的特定页面?
【发布时间】:2012-12-24 10:19:38
【问题描述】:

您好,我正在使用FluentSecurity 在我的 MVC 应用程序中验证和验证用户权限。在基本设置中,当用户想要访问被拒绝的Action 时,它会引发异常。我想知道我应该如何重定向到另一个页面(例如登录页面)而不是显示黄色异常页面?

【问题讨论】:

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


    【解决方案1】:

    我知道这个问题已经得到解答,但我不喜欢在处理这种情况的每个操作中都尝试捕获。

    Fluent Security 允许您为违反策略的情况注册处理程序(请参阅https://github.com/kristofferahl/FluentSecurity/wiki/Policy-violation-handlers)。您必须有一个从 IPolicyViolationHandler 继承的类。惯例是将你的班级命名为<PolicyViolationName>PolicyViolationHandler

    这是一个注册 DenyAnonymousAccessPolicyViolationHandler 的处理程序示例

        /// <summary>
        /// Custom Policy Violation Handler. See http://www.fluentsecurity.net/wiki/Policy-violation-handlers
        /// </summary>
        public class DenyAnonymousAccessPolicyViolationHandler : IPolicyViolationHandler
        {
            public ActionResult Handle(PolicyViolationException exception)
            {
                Flash.Error("You must first login to access that page");
                return new RedirectResult("/");
            }
        }
    

    您将遇到的另一个警告是您必须使用 IOC 容器来注册这些处理程序。我不会争论使用和 IOC 容器是好是坏,但如果我没有,我宁愿不使用。在他们的网站上有一篇关于如何在不使用 IOC 容器的情况下执行此操作的博客,但我也不太喜欢这种方法。这就是我所做的。

    public static class SecurityConfig
        {
            public static void Configure()
            {
                SecurityConfigurator.Configure(c =>
                    {
                        c.GetAuthenticationStatusFrom(() => HttpContext.Current.User.Identity.IsAuthenticated);
                        c.GetRolesFrom(() => (HttpContext.Current.Session["Roles"] as string[]));
                            // Blanked Deny All
                        c.ForAllControllers().DenyAnonymousAccess();
    
                        // Publicly Accessible Areas
                        c.For<LoginController>().Ignore();
    
                        // This is the part for finding all of the classes that inherit
                        // from IPolicyViolationHandler so you don't have to use an IOC
                        // Container.
                        c.ResolveServicesUsing(type =>
                            {
                                if (type == typeof (IPolicyViolationHandler))
                                {
                                    var types = Assembly
                                        .GetAssembly(typeof(MvcApplication))
                                        .GetTypes()
                                        .Where(x => typeof(IPolicyViolationHandler).IsAssignableFrom(x)).ToList();
    
                                    var handlers = types.Select(t => Activator.CreateInstance(t) as IPolicyViolationHandler).ToList();
    
                                    return handlers;
                                }
                                return Enumerable.Empty<object>();
                            });
                    });
            }
        }
    

    【讨论】:

      【解决方案2】:

      我从不使用FluentSecurity,但您可以按照这种方式重定向您的操作。例如;

      public ActionResult YourActionName()
      {
         try
         {
      
         }
         catch ( Exception )
         {       
             return RedirectToAction("Index", "Home");
         }            
      }
      

      您还可以在控制器类上使用HandleError 属性来捕获任何未处理的异常,它会自动返回共享文件夹中的Error.aspx 视图。你也可以自定义它。

      有关更多信息,请查看 ScottGu 的帖子。 http://weblogs.asp.net/scottgu/archive/2008/07/14/asp-net-mvc-preview-4-release-part-1.aspx

      【讨论】:

      • 感谢您的回复,还有其他内置功能吗?
      • 看看我的回答:)
      【解决方案3】:

      目前FluentSecurity 稳定版(1.4) 没有任何内置功能来处理PolicyViolationException,但您可以创建一个过滤器来执行此操作,如下所示:

      public class PolicyViolationExceptionHandler : IExceptionFilter
      {
          public void OnException(ExceptionContext filterContext)
          {
              if (filterContext.Exception.GetType() == typeof(PolicyViolationException))
              {
                  var routeDictionary = new RouteValueDictionary(new
                  {
                      area = "",
                      controller = "account",
                      action = "login"
                  });
      
                  // Redirect to specific page
                  filterContext.HttpContext.Response.RedirectToRoute(routeDictionary);
      
                  // Prevent to handle exceptions
                  // Of 'PolicyViolationException' by default filters
                  filterContext.ExceptionHandled = true;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-21
        • 2010-12-06
        • 1970-01-01
        • 2022-06-18
        • 1970-01-01
        相关资源
        最近更新 更多