title: How to login to ASP.Net MVC website (Forms authentication) using HttpClient in Windows Store App?
body: <p>First I created the HttpClientHandler with cookie container</p>
  
  <pre><code>CookieContainer cookies = new CookieContainer();
  HttpClientHandler handler = new HttpClientHandler();
  handler.CookieContainer = cookies;
  handler.UseCookies = true;
  var hc = new HttpClient(handler);
  </code></pre>
  
  <p>Then I hit the Base Url just to get the cookie with "__RequestVerificationToken"</p>
  
  <pre><code>string r = await hc.GetStringAsync(BaseUrl);
  </code></pre>
  
  <p>Then I post the username/password to the login Url</p>
  
  <pre><code>HttpContent content = new FormUrlEncodedContent(new[]
  {
      new KeyValuePair&lt;string, string&gt;("UserName", "admin"),
      new KeyValuePair&lt;string, string&gt;("Password", password),
  });
  HttpResponseMessage response = await hc.PostAsync(LoginUrl, content);
  </code></pre>
  
  <p>Then I get the server error "The required anti-forgery form field &quot;__RequestVerificationToken&quot; is not present".</p>
  
  <p>But when I check the request in fiddler, I can see that "__RequestVerificationToken" is already added in the cookies of the request.</p>
  
  <p>Then I tried to login manually in IE, and check what kind of the request IE sent.</p>
  
  <p>Then I discovered IE also put "__RequestVerificationToken" in the form, so I added the cookie in the form</p>
  
  <pre><code>new KeyValuePair&lt;string, string&gt;("__RequestVerificationToken", cookies.GetCookies(new Uri(BaseUrl)).Cast&lt;Cookie&gt;().FirstOrDefault(x =&gt; x.Name == "__RequestVerificationToken").Value)
  </code></pre>
  
  <p>Then I got this error</p>
  
  <p>"Validation of the provided anti-forgery token failed. The cookie &quot;_<em>RequestVerificationToken&quot; and the form field &quot;</em>_RequestVerificationToken&quot; were swapped."</p>
  
  <p>Then I couldn't get any search result on google for this error.</p>
  
  <p>Any idea?</p>
  
  <p>Thanks</p>