Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The anti-forgery cookie token and form field token do not match in MVC 4

I'm using the default login module in ASP.NET MVC 4. I did not change any code in the default application and i hosted it on a shared server.

After i logged in using default login page. i kept the browser idle for some time. Then obviously application redirected to the login page when i try to perform any controller action with [Authorize] attribute.

Then i try to login again and it gives an error when i click on login button.

The anti-forgery cookie token and form field token do not match. 

enter image description here

LogIn action

// POST: /Account/Login          [HttpPost]         [AllowAnonymous]         [ValidateAntiForgeryToken]         public ActionResult Login(LoginModel model, string returnUrl)         {             if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))             {                 return RedirectToLocal(returnUrl);             }              // If we got this far, something failed, redisplay form             ModelState.AddModelError("", "The user name or password provided is incorrect.");             return View(model);         } 
like image 932
chamara Avatar asked Aug 07 '13 07:08

chamara


People also ask

What is anti-forgery token in MVC?

To help prevent CSRF attacks, ASP.NET MVC uses anti-forgery tokens, also called request verification tokens. The client requests an HTML page that contains a form. The server includes two tokens in the response. One token is sent as a cookie. The other is placed in a hidden form field.

What is anti-forgery cookie token?

Anti-forgery token's main purpose is to prevent attacker using authentication cookie for doing things on behalf of the actual user. Since the user isn't authenticated yet in the login page, there are customers removing the validation.

What is Aspnetcore Antiforgery cookie?

Cross-site request forgery (also known as XSRF or CSRF) is an common attack against web apps that store authentication tokens in the cookies. Browser will automatically attach these authentication cookies with every request to the website.


2 Answers

I resolved the issue by explicitly adding a machine key in web.config.

Note: For security reason don't use this key. Generate one from https://support.microsoft.com/en-us/kb/2915218#AppendixA. Dont use online-one, details, http://blogs.msdn.com/b/webdev/archive/2014/05/07/asp-net-4-5-2-and-enableviewstatemac.aspx

 <machineKey validationKey="971E32D270A381E2B5954ECB4762CE401D0DF1608CAC303D527FA3DB5D70FA77667B8CF3153CE1F17C3FAF7839733A77E44000B3D8229E6E58D0C954AC2E796B" decryptionKey="1D5375942DA2B2C949798F272D3026421DDBD231757CA12C794E68E9F8CECA71" validation="SHA1" decryption="AES" /> 

Here's a site that generates unique Machine Keys:

http://www.developerfusion.com/tools/generatemachinekey/

like image 86
chamara Avatar answered Sep 18 '22 17:09

chamara


Another reason for having this error is if you are jumping between [Authorize] areas that are not cached by the browser (this would be done on purpose in order to block users from seeing protected content when they sign out and using the back button for example).

If that's case you can make your actions non cached, so if someone click the back button and ended up on a form with @Html.AntiForgeryToken() the token will not be cached from before.

See this post for how to add [NoCache] ActionFilterAttribute: How to handle form submission ASP.NET MVC Back button?

like image 20
Yovav Avatar answered Sep 18 '22 17:09

Yovav