title: MVC4 - Android authentication via http POST and FormsAuthentication
body: <p>I know there are plenty of resources like this on the web, and the closest I've come was the answer to this question: <a href="https://stackoverflow.com/questions/11014953/asp-net-web-api-authentication">ASP.NET Web API Authentication</a>.</p>
  
  <p>Basically, this is my requirement. Log in via android to my account on an MVC4 internet application I created (which uses SimpleMembership). It is NOT an MVC Web Api app, which seems to confuse things when looking at the various ways of achieving this. </p>
  
  <p>I am attempting to use FormsAuthentication to set an authentication cookie, but I have no idea how to configure my android httpclient to actually send through this authentication cookie, or how to get MVC to save a session from my android app.</p>
  
  <p>So far, this is what I've come up with on the MVC side:</p>
  
  <pre><code>    [HttpPost]
      [AllowAnonymous]
      public bool LoginMobi(LoginModel model)
      {    
          var membership = (SimpleMembershipProvider)Membership.Provider;
          if (membership.ValidateUser(model.UserName, model.Password))
          {
              FormsAuthentication.SetAuthCookie(model.UserName, false);            
              return true;
          }
          else return false;
      }
  </code></pre>
  
  <p>And I use the following java in my android app (sent over an SSL connection):</p>
  
  <pre><code>            DefaultHttpClient httpclient = new DefaultHttpClient();
  
              HttpPost httppost = new HttpPost("https://mysite/api/login");
              List&lt;NameValuePair&gt; nameValue = new ArrayList&lt;NameValuePair&gt;();
              nameValue.add(new BasicNameValuePair("UserName", "foo"));
              nameValue.add(new BasicNameValuePair("Password", "bar"));
              httppost.setEntity(new UrlEncodedFormEntity(nameValue));
              httppost.setHeader("Content-type", "application/json");
              HttpResponse response = httpclient.execute(httppost);
              // etc etc
  </code></pre>
  
  <p>What I haven't figured out is how to receive the authentication cookie on android and send it back with each request to controllers with the <code>[Authorize]</code> attribute. I'm rather new to this so please forgive my ignorance!</p>