设置 AntiForgeryToken 以重定向到 C# 中的 HttpGet 方法
Set AntiForgeryToken for redirect to HttpGet method in C#
我想使用 [ValidateAntiForgeryToken] 属性检查我的网络应用程序中的 CSRF。问题是,我有一个更改数据的 GET 方法,所以它实际上应该是 POST。但除了在相应表单中提交到此操作方法外,还有多个重定向到此方法,因此我无法将其更改为 POST 方法。据我所知,[ValidateAntiForgeryToken] 不适用于 GET 方法。是否有另一种方法来验证 GET 方法或在代码内重定向到 POST 方法而不使用表单?
我的操作方法如下所示:
public ActionResult SomeAction(SomeModel model)
{
// changes are made in database!!
return View("View", model);
}
并且此操作方法正在从另一个操作方法重定向到:
public ActionResult SomeOtherAction()
{
return RedirectToAction("SomeAction", "Controller");
}
我想将第一个操作更改为:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SomeAction(SomeModel model)
{
// changes are made in database!!
return View("View", model);
}
并在相应视图中添加@Html.AntiForgeryToken。但是第二个动作中的重定向将不再起作用。有谁知道解决这个问题的方法吗?
您可以return someAction 方法...
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpGet]
[ValidateAntiForgeryToken]
public ActionResult SomeOtherAction()
{
return SomeAction();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SomeAction()
{
return View("SomeAction");
}
}
Index.cshtml
@using (Html.BeginForm("SomeOtherAction", "Home", FormMethod.Get))
{
@Html.AntiForgeryToken();
<button type="submit">Click Me</button>
}
我想使用 [ValidateAntiForgeryToken] 属性检查我的网络应用程序中的 CSRF。问题是,我有一个更改数据的 GET 方法,所以它实际上应该是 POST。但除了在相应表单中提交到此操作方法外,还有多个重定向到此方法,因此我无法将其更改为 POST 方法。据我所知,[ValidateAntiForgeryToken] 不适用于 GET 方法。是否有另一种方法来验证 GET 方法或在代码内重定向到 POST 方法而不使用表单?
我的操作方法如下所示:
public ActionResult SomeAction(SomeModel model)
{
// changes are made in database!!
return View("View", model);
}
并且此操作方法正在从另一个操作方法重定向到:
public ActionResult SomeOtherAction()
{
return RedirectToAction("SomeAction", "Controller");
}
我想将第一个操作更改为:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SomeAction(SomeModel model)
{
// changes are made in database!!
return View("View", model);
}
并在相应视图中添加@Html.AntiForgeryToken。但是第二个动作中的重定向将不再起作用。有谁知道解决这个问题的方法吗?
您可以return someAction 方法...
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpGet]
[ValidateAntiForgeryToken]
public ActionResult SomeOtherAction()
{
return SomeAction();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SomeAction()
{
return View("SomeAction");
}
}
Index.cshtml
@using (Html.BeginForm("SomeOtherAction", "Home", FormMethod.Get))
{
@Html.AntiForgeryToken();
<button type="submit">Click Me</button>
}