title: ASP.NET MVC5 : Turn off Windows Authentication on a single page
body: <p>My website (ASP.NET MVC5 with Entity Framework) use the Windows Authentication system. It is very useful and enable me to get more information about the user by searching an Active Directory. I can automatically fill form fields with information like e-mail, ... </p>
  
  <p><strong>Web.config:</strong></p>
  
  <pre><code>[...]
  &lt;authentication mode="Windows" /&gt;
  &lt;authorization&gt;
    &lt;deny users="?" /&gt;
  &lt;/authorization&gt;
  [...]
  </code></pre>
  
  <p>I have a Powershell script (that do another job) which must access to one page of the site to send some information. I need to provide this access to the script, without any authentication (turn off Windows Authentication only for this Action/Page). I used at first the attribute AllowAnonymous, that had <strong>no effect</strong>.</p>
  
  <p>Action in my <strong>controller</strong>:</p>
  
  <pre><code>[AllowAnonymous]
      public ActionResult ReceiveData(string scriptVersion, string content)
      {
          try
          {
              if (scriptVersion != null &amp;&amp; content != null)
              {
                  HttpUtility.UrlDecode(content);                    
                  Xxxxx.UpdatexxxxxxServer(content); // I just replaced the name of my project
                  return new HttpStatusCodeResult(200, "OK");
              }
  
              return new HttpStatusCodeResult(403, "You're not allowed to do this.");
          }
          catch(Exception e)
          {
              return new HttpStatusCodeResult(400, "Unable to execute this request on the DB. Detailed error: " + e.Message);
          }            
  
      }
  </code></pre>
  
  <p>Powershell script (only for info, no need to understand it for this question purpose):</p>
  
  <pre><code>$url = "http://localhost:3349/Xxxxxx/ReceiveData"
  $parameters = "scriptVersion=2&amp;content=$([System.Web.HttpUtility]::UrlEncode($(Get-Content "$scriptFile")))"
  
  $http_request = New-Object -ComObject Msxml2.XMLHTTP
  $http_request.open('POST', $url, $false)
  $http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
  $http_request.setRequestHeader("Content-length", $parameters.length)
  $http_request.setRequestHeader("Connection", "close")
  $http_request.send($parameters)
  </code></pre>
  
  <p>When my script reach the send method, He ask me for credential, and I don't want to provide each time credentials (And i don't want to put my credentials in the script). That is why I expected to disable the Windows Authentication on this Action.</p>
  
  <p>I've done some research, trying to find any working solution. I saw this subject : <a href="https://stackoverflow.com/questions/19551113/mvc-5-0-allowanonymous-and-the-new-iauthenticationfilter">MVC 5.0 [AllowAnonymous] and the new IAuthenticationFilter</a> 
  AllowAnonymous attribute seems to not working in MVC5 with the addition of new filter. That's why I have tried to add a custom filter:</p>
  
  <pre><code>public class CustomAuthenticationAttribute : ActionFilterAttribute, IAuthenticationFilter 
  {        
      public void OnAuthentication(AuthenticationContext filterContext)
      {
  
      }
  
      public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
      {
          var user = filterContext.HttpContext.User;
          if (user == null || !user.Identity.IsAuthenticated)
          {
              filterContext.Result = new HttpUnauthorizedResult();
          }
      }
  }
  </code></pre>
  
  <p>I added it on the FilterConfig and put the CustomAuthenticationAttribute on my Action ReceiveData. <strong>No better result</strong>... All the action are going in this filter but I can't disable Windows Authentication on my Action.</p>
  
  <p>My modified action:</p>
  
  <pre><code>[AllowAnonymous]
  [CustomAuthenticationAttribute]
  public ActionResult ReceiveData(string scriptVersion, string content) {
      [...]
  }
  </code></pre>
  
  <p>I'm not an expert and it would be cool if you can help me find the solution and understand it. Thanks in advance and sorry for the english mistakes, this is not my native language.</p>