Do you use Anti Forgery Tokens on any page that takes a POST?
Rules to Better MVC|f6113a94-ea9d-4c29-9f67-4770573c432d
v1.0
Posted at
9/03/2013 2:02 AM by
Tiago Araujo
Rule Intro
To prevent cross-site request forgery (XSRF), you should use Html.AntiForgeryToken. On the action which takes the post request, place the ValidateAntiForgeryToken attribute to enable the request to validate. Doing this ensures that the post is a direct response to the page that was given to this user so only verified posts will be processed.
Page Content
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<p>
<input type="submit" value="Create" />
</p>
}
- Figure: Bad Example – The page is potentially vulnerable to XSRF attacks. Any post will be accepted by the server
View:
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<p>
<input type="submit" value="Create"/>
</p>
}
Controller:
[ValidateAntiForgeryToken]
public ActionResult Create(CreateModel model)
{
// save data
}
- Figure: Good Example – The page is no longer vulnerable to XSRF attacks
{4D370343-7442-4305-B1EE-1D175F4184F4}
Do you feel this rule needs an update?