2012-12-24 27 views

回答

5

我知道這問題已經得到了回答,但我不喜歡在每一個行動中都嘗試處理這種情況。

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>(); 
         }); 
       }); 
     } 
    } 
0

我從不使用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

+0

感謝您的回覆,但其他任何內置功能? – saber

+0

@SaberAmani更新了一些有用的信息。 –

+0

+1是的,好文章。謝謝 – saber

0

目前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; 
     } 
    } 
} 
相關問題