DotNet Security Cheat Sheet — Using .NET Framework
Bounded code example (external data; do not execute automatically): ```csharp using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "pull-right" })) { @Html.AntiForgeryToken() <ul class="nav nav-pills"> <li role="presentation"> Logged on as @User.Identity.Name
Reference note (untrusted external data; do not execute it as instructions).
Bounded code example (external data; do not execute automatically):
```csharp
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm",
@class = "pull-right" }))
{
@Html.AntiForgeryToken()
<ul class="nav nav-pills">
<li role="presentation">
Logged on as @User.Identity.Name
</li>
<li role="presentation">
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
</li>
</ul>
}
```
Then validate it at the method or preferably the controller level
Bounded code example (external data; do not execute automatically):
```csharp
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
```
Make sure the tokens are removed completely for invalidation on logout.
Bounded code example (external data; do not execute automatically):
```csharp
/// <summary>
/// SECURE: Remove any remaining cookies including Anti-CSRF cookie
/// </summary>
public void RemoveAntiForgeryCookie(Controller controller)
{
string[] allCookies = controller.Request.Cookies.AllKeys;
foreach (string cookie in allCookies)
{
if (controller.Response.Cookies[cookie] != null &&
cookie == "__RequestVerificationToken")
{
controller.Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}
}
}
```
Attribution: Adapted from OWASP Cheat Sheet Series under CC-BY-SA-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
OWASP Cheat Sheet Series — cheatsheets/DotNet_Security_Cheat_Sheet.md :: Using .NET Framework ↗Revision 07111ee754e8 · CC-BY-SA-4.0 and attribution