嗨我正在使用FluentSecurity在我的MVC應用程序中驗證和驗證用戶權限。在基本設置中,當用戶想訪問拒絕Action
時,它會引發異常。我想知道我應該如何重定向到另一個頁面(例如登錄頁面)而不是顯示黃色的異常頁面?如何重定向到FluentSecurity中的特定頁面?
3
A
回答
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
目前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;
}
}
}
相關問題
- 1. 如何重定向到特定頁面?
- 2. 如何將特定頁面重定向到我的404頁面?
- 3. htaccess將特定頁面的特定頁面重定向到特定頁面
- 4. htaccess將特定頁面的特定頁面重定向到特定頁面
- 5. 如何重定向到asp.net中的特定網站頁面?
- 6. 重定向到特定頁面
- 7. 域名重定向到特定頁面
- 8. PHP不重定向到特定頁面
- 9. 將特定頁面重定向到iis web.config中的另一個特定頁面
- 10. 如何從asp.net中的JS顯示頁面重定向到特定頁面
- 11. 如何重定向到codeigniter頁面的特定部分
- 12. 如何重定向到頁面的特定部分?
- 13. 如何在登錄到WordPress的特定頁面後重定向?
- 14. 如何重定向到特定的錯誤頁面在MVC FileDownloadResult
- 15. 的.htaccess重定向頁面未找到特定頁面
- 16. 如何使用mod_rewrite將特定的URL重定向到特定的頁面?
- 17. Rails/Devise如何在登錄後重定向到特定頁面
- 18. React REDX如何使用react-router重定向到特定頁面
- 19. 如何在頁面刷新時將頁面重定向到特定的URL?
- 20. 如何從Sinatra的特定頁面重定向網頁?
- 21. 301將任意頁面重定向到一個特定頁面
- 22. 如何重定向頁面
- 23. 如何重定向頁面?
- 24. 使用htaccess重定向特定頁面
- 25. 在特定時間重定向頁面
- 26. 301重定向特定頁面
- 27. 從特定頁面重定向
- 28. 將用戶重定向到web視圖中的特定頁面
- 29. 將所有請求重定向到Wicket中的特定頁面
- 30. 從php函數中的javascript重定向到特定頁面
感謝您的回覆,但其他任何內置功能? – saber
@SaberAmani更新了一些有用的信息。 –
+1是的,好文章。謝謝 – saber