The anti-forgery token could not be decrypted error in ASP .Net MVC

The anti-forgery token could not be decrypted. If this application is hosted by a Web Farm or cluster, ensure that all machines are running the same version of ASP.NET Web Pages and that the <machineKey> configuration specifies explicit encryption and validation keys. AutoGenerate cannot be used in a cluster.

 

The above error will happen when we have multiple AntiForgeryToken in same form and we try to submit the form Or any object that is submitting to action that has multiple AntiForgeryToken (This case will happen if we try to submit FormData or serialized data ) in case of ajax calls. This is a ASP MVC error it can happen while we creating forms.


For Example

<form id="dummy"  action="/Admin/Product/Create" method="post">

 @Html.AntiForgeryToken()

 <label  >Product Name</label>
 <input id="txtName" name="ProductName" type="text" placeholder="Product Name" >
 @Html.AntiForgeryToken()				
 <button id="btnSave" name="btnSave" >Save</button>									
         
</form>									

 

In the above example, we can see the form has two call to generate Anti Forgery Token so when we submit the form we will get the error “The anti-forgery token could not be decrypted”.

 

The view ‘Index’ or its master was not found or no view engine supports the searched locations

Problem

The view ‘Index’ or its master was not found or no view engine supports the searched locations. The following locations were searched:

  • ~/Views/Home/Index.aspx
  • ~/Views/Home/Index.ascx
  • ~/Views/Shared/Index.aspx
  • ~/Views/Shared/Index.ascx
  • ~/Views/Home/Index.cshtml
  • ~/Views/Home/Index.vbhtml
  • ~/Views/Shared/Index.cshtml
  • ~/Views/Shared/Index.vbhtml

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Solution

            The problem will occurs when we trying to access an action in the controller.  Consider the below action

public class HomeController : Controller
{
           public ActionResult Index()
           {
                 return View();
            }
}

The action index returns a view. Then it will check the corresponding view file in the following locations 

~/Views/Home/

 ~/Views/Shared/

Not only the location, it will check corresponding view with the action name that is Index is in our case, with following extensions

  • .aspx
  • .ascx
  • .aspx
  • .ascx
  • .cshtml
  • .vbhtml
  • .cshtml
  • .vbhtml

In the folder home or shared should contain a file with any of the extension above. 

After creating the view in the specified folder the controller will get the correct view, then view will be shown in the page and we can avoid this error. This is one of the error we face when we start mvc projects. But is a basic thing in mvc in .net, it will understood this with in some days after starting study of mvc in .net.