Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何重定向到FluentSecurity中的特定页面?_C#_Asp.net_.net_Asp.net Mvc_Fluent Security - Fatal编程技术网

C# 如何重定向到FluentSecurity中的特定页面?

C# 如何重定向到FluentSecurity中的特定页面?,c#,asp.net,.net,asp.net-mvc,fluent-security,C#,Asp.net,.net,Asp.net Mvc,Fluent Security,您好,我使用它来验证和验证MVC应用程序中的用户权限。在“基本设置”中,当用户想要访问被拒绝的操作时,它会引发异常。我想知道我应该如何重定向到另一个页面(例如登录页面),而不是显示黄色的异常页面?我从不使用FluentSecurity,但您可以按照这种方式在操作中重定向。比如, public ActionResult YourActionName() { try { } catch ( Exception ) { return Redi

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

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

public ActionResult YourActionName()
{
   try
   {

   }
   catch ( Exception )
   {       
       return RedirectToAction("Index", "Home");
   }            
}
您还可以使用控制器类上的属性捕获任何未处理的异常,它将自动返回共享文件夹中的
Error.aspx
视图。您还可以自定义它

有关更多信息,请查看ScottGu的帖子。

当前稳定版本(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;
        }
    }
}

我知道这个问题已经得到了回答,但我不喜欢在处理这种情况的每一个行动中都尝试一下

Fluent Security允许您注册策略冲突的处理程序(请参阅)。您必须有一个从IPolicyViolationHandler继承的类。惯例是命名类
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("/");
        }
    }
//
///自定义策略冲突处理程序。看见http://www.fluentsecurity.net/wiki/Policy-violation-handlers
/// 
公共类DenyAnonymousAccessPolicyViolationHandler:IPolicyViolationHandler
{
公共操作结果句柄(PolicyViolationException异常)
{
错误(“您必须先登录才能访问该页面”);
返回新的重定向结果(“/”);
}
}
您将遇到的另一个警告是,您必须使用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>();
                        });
                });
        }
    }
public静态类SecurityConfig
{
公共静态void Configure()
{
SecurityConfiguration.Configure(c=>
{
c、 GetAuthenticationStatusFrom(()=>HttpContext.Current.User.Identity.IsAuthenticated);
c、 GetRolesFrom(()=>(HttpContext.Current.Session[“角色”]作为字符串[]);
//否认一切
c、 ForAllControllers().DenyAnonymousAccess();
//公众可进入的区域
c、 For().Ignore();
//这是查找继承的所有类的部分
//从IPolicyViolationHandler,因此您不必使用IOC
//容器。
c、 ResolveServicesUsing(类型=>
{
if(type==typeof(IPolicyViolationHandler))
{
变量类型=程序集
.GetAssembly(类型(MVCAPApplication))
.GetTypes()
其中(x=>typeof(IPolicyViolationHandler).IsAssignableFrom(x)).ToList();
var handlers=types.Select(t=>Activator.CreateInstance(t)作为IPolicyViolationHandler.ToList();
返回处理程序;
}
返回可枚举的.Empty();
});
});
}
}

谢谢您的回复,但还有其他内置功能吗?请看我的答案:)