我已經implimented的AnitforgeryToken與我的asp.net MVC形式,以及添加的屬性,以我的登錄程序,但是當檢查failes我想重定向到我的欺詐行爲,而不是一個例外頁面。這是可能的嗎?asp.net MVC antiforgerytoken上異常RedirectToAction
感謝
我已經implimented的AnitforgeryToken與我的asp.net MVC形式,以及添加的屬性,以我的登錄程序,但是當檢查failes我想重定向到我的欺詐行爲,而不是一個例外頁面。這是可能的嗎?asp.net MVC antiforgerytoken上異常RedirectToAction
感謝
的ValidateAntiForgeryTokenAttribute只會扔HttpAntiForgeryException。您可以使用HandleErrorAttribute來處理這種情況:
[HandleError(
ExceptionType = typeof(HttpAntiForgeryException),
View = "Unauthorized")]
[ValidateAntiForgeryToken]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SomeActionThatRequiresToken()
{
return View();
}
如果你不想把[HandleError]
屬性上有[ValidateAntiForgeryToken]
所有行動,你可以自定義過濾器添加到您的全局過濾器:
在Global.asax中下Application_Start()
:
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
然後:
個public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new AntiForgeryTokenFilter());
}
}
AntiForgeryTokenFilter.cs:
public class AntiForgeryTokenFilter : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if(filterContext.Exception.GetType() == typeof(HttpAntiForgeryException))
{
filterContext.Result = new RedirectResult("/"); // whatever the url that you want to redirect to
filterContext.ExceptionHandled = true;
}
}
}
評論爲什麼你失望投票。 – Dmitry 2013-03-15 17:57:01
三江源多數民衆贊成我一直在尋找。 – davethecoder 2009-11-07 14:08:31