在这里,我有两个表单帖子的简单 MVC3 应用程序。为了保护 CSRF 攻击,我按照此处的指导以两种形式使用了 antiforgerytoken html 助手。
这是我的两个模型:
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
}
这是我的 homeController.cs:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(User user)
{
if (ModelState.IsValid)
return RedirectToAction("About");
return View();
}
public ActionResult About()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult About(Employee employee)
{
if (ModelState.IsValid)
return RedirectToAction("PageA");
return View();
}
}
这是我的 Inex.cshtml:
@model MvcAntiforgeryToken.Models.User
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div>
<fieldset>
<legend>User Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.FirstName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.FirstName)
@Html.ValidationMessageFor(m => m.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.LastName)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.LastName)
@Html.ValidationMessageFor(m => m.LastName)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
</div>
}
这是我的 About.cshtml:
@model MvcAntiforgeryToken.Models.Employee
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div>
<fieldset>
<legend>Employee Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.Id)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Id)
@Html.ValidationMessageFor(m => m.Id)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Name)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Name)
@Html.ValidationMessageFor(m => m.Name)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
</div>
}
主页/索引的发布:
当用户访问主页/索引,应用程序创建的“ RequestVerificationToken_Lw具有值”饼干“PG2 / E00Q2DngYxs98f92x9qqrIvrh6zCT / + GGte67NFZLazKFlz ++ QqMSHpkZ08Qum9vsBCtq7O7MSzCawJkEa2 / hdjrWoAcHlDWxxYRWKXm + OxPbqlRs609zam4fK7hReGEX3zf8YR4ltH3oYf4AZgt2mZV31ihRGShiZ7Oy9k =”
并遵循隐藏的表单输入
<input name="__RequestVerificationToken" type="hidden" value="B1KKzYEFEdINnuhy53MqqxHCHELPUd5pX3vRqYWz1+pkhBA6YGFvSVtXgSURkAn3yNwee3nrqDCMXB8MB0SWiUU3GuHnhH7+Qc1IQebJHoFJZR2CPXNOmUzINXbBWKZz+35pQQQXdiKptR3raLSoElfQi18ZC4Pr7xNREGIOM2A=" />
发布主页/关于:
当用户访问首页/公司,应用程序创建“ RequestVerificationToken_Lw与价值的” cookie “PG2 / E00Q2DngYxs98f92x9qqrIvrh6zCT / + GGte67NFZLazKFlz ++ QqMSHpkZ08Qum9vsBCtq7O7MSzCawJkEa2 / hdjrWoAcHlDWxxYRWKXm + OxPbqlRs609zam4fK7hReGEX3zf8YR4ltH3oYf4AZgt2mZV31ihRGShiZ7Oy9k =”
并遵循表单输入
<input name="__RequestVerificationToken" type="hidden" value="UOCMATdy93A0230aBmRPv5F0xpJlI2urE5sJ4nxsTSWrsi9/xM5qhrxQ4I2vWIjvVrhkW8gSgmGFp7c4XPQUQG5myMGipTAr2/mi5od+Sz6IcfrF2FxwjfWMslt96BcMG6b9BjaGbgnClQOVTkjfHEMIptOYUCTSbVK61dWp5qI=" />
这是我的问题:
为什么两种形式的“ RequestVerificationToken_Lw ”cookie 值相同?不应该为每个表单帖子重新创建它吗?
为什么“ RequestVerificationToken_Lw ”cookie 值和“__RequestVerificationToken”隐藏输入值不同?
非常感谢您的回复!