Created
December 13, 2013 15:18
-
-
Save NeilRobbins/7945777 to your computer and use it in GitHub Desktop.
CSRF session linked protection with ASP.NET MVC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Web; | |
| using System.Web.Helpers; | |
| namespace CSRF.Infrastructure | |
| { | |
| public class AntiForgerySessionDataProvider : IAntiForgeryAdditionalDataProvider | |
| { | |
| private readonly Func<string, string> _sessionKeyValuePair = sessionId => string.Format("Session: {0}", sessionId); | |
| public string GetAdditionalData(HttpContextBase context) | |
| { | |
| return _sessionKeyValuePair(context.Session.SessionID); | |
| } | |
| public bool ValidateAdditionalData(HttpContextBase context, string additionalData) | |
| { | |
| return additionalData == _sessionKeyValuePair(context.Session.SessionID); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections.Generic; | |
| using System.Web.Mvc; | |
| namespace CSRF.Controllers | |
| { | |
| public class CsrfTestController : Controller | |
| { | |
| public ActionResult Index() | |
| { | |
| return View(Memory.Mem); | |
| } | |
| [ValidateAntiForgeryToken] | |
| public ActionResult Create(string someVal) | |
| { | |
| Memory.Mem.Add(someVal); | |
| return RedirectToAction("Index"); | |
| } | |
| } | |
| public static class Memory | |
| { | |
| public static HashSet<string> Mem = new HashSet<string>(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Web.Helpers; | |
| using System.Web.Http; | |
| using System.Web.Mvc; | |
| using System.Web.Optimization; | |
| using System.Web.Routing; | |
| using CSRF.Infrastructure; | |
| namespace CSRF | |
| { | |
| public class WebApiApplication : System.Web.HttpApplication | |
| { | |
| protected void Application_Start() | |
| { | |
| AreaRegistration.RegisterAllAreas(); | |
| WebApiConfig.Register(GlobalConfiguration.Configuration); | |
| FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); | |
| RouteConfig.RegisterRoutes(RouteTable.Routes); | |
| BundleConfig.RegisterBundles(BundleTable.Bundles); | |
| AntiForgeryConfig.AdditionalDataProvider = new AntiForgerySessionDataProvider(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @model HashSet<string> | |
| @{ | |
| ViewBag.Title = "title"; | |
| } | |
| <h2>title</h2> | |
| <ul> | |
| @foreach (var m in Model) | |
| { | |
| <li>@m</li> | |
| } | |
| </ul> | |
| @using (Html.BeginForm("Create", "CsrfTest", FormMethod.Post)) | |
| { | |
| <input name="someVal" type="text"/> | |
| @Html.AntiForgeryToken() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment