title: C# Razor Html.AntiForgeryToken throws System.ObjectDisposedException safe handle is closed
body: <p>Trying to apply the C# Razor AntiForgeryToken to some Ajax calls in my app due to security audit recommendations and after trying to implement many of the solutions on the following stack question:
  <a href="https://stackoverflow.com/questions/14473597/include-antiforgerytoken-in-ajax-post-asp-net-mvc">include antiforgerytoken in ajax post ASP.NET MVC</a></p>
  <p>I always get the same error, on the first time I call Html.AntiForgeryToke() it opens successfully, but then when opening a second Page or the same Page a second time <code>Html.AntiForgeryToken()</code> throws <code>System.ObjectDisposedException: safe handle is closed</code></p>
  <p>How can I add an AntiForgeryToken to every form/page, if the Html.AntiForgeryToken() method can only be called once?</p>
  <p>Here's some sample of the current implementation (used this literally), lets call it the Index page, where I have the following hidden Form:</p>
  <pre><code>@using (Html.BeginForm(null, null, FormMethod.Post, new { id = &quot;__AjaxAntiForgeryForm&quot; }))
  {
      @Html.AntiForgeryToken()
  }
  </code></pre>
  <p>Where I have also a button/icon with the following JS function (changed method names and var names only)</p>
  <pre><code>$('#myIcon').click(function () {
  
      var form = $('#__AjaxAntiForgeryForm');
      var dataTosend = { __RequestVerificationToken: $('input[name=&quot;__RequestVerificationToken&quot;]', form).val() };
  
      myAjaxPostUtilFunc('@Url.Action(&quot;OpenAPopupAction&quot;, &quot;MyControllerName&quot;)', dataTosend, function (data) {
          myReturnAction(data); //will open a popup with stuff
  
      }, { type: &quot;POST&quot;, dataType: &quot;html&quot; });
  });
  </code></pre>
  <p>Then navigating a 2nd time to my Index page throws exception.</p>
  <p>EDIT (added mvc version in tags for clarity): Found a reference to a similar issue: <a href="https://stackoverflow.com/questions/37511452/mvc-6-generates-antiforgerytoken-only-once">mvc 6 generates antiforgerytoken only once</a>
  The bug seems to be present on MVC 5 and 6 (and maybe core v1). Currently attempting the solutions on this other Post, will share if they work later.</p>