2014-10-11 102 views
1

我知道網上有很多這樣的資源,而我最接近的就是這個問題的答案:ASP.NET Web API AuthenticationMVC4 - 通過http POST和FormsAuthentication進行Android身份驗證

基本上這是我的要求。在我創建的MVC4互聯網應用程序(使用SimpleMembership)上通過android登錄到我的帳戶。它不是一個MVC Web Api應用程序,它在查看實現此目的的各種方法時似乎會混淆視聽。

我試圖使用FormsAuthentication來設置身份驗證Cookie,但我不知道如何配置我的android httpclient實際發送通過此身份驗證cookie,或如何獲得MVC從我的android應用程序保存會話。

到目前爲止,這是我想出的MVC方面:

[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; 
    } 

我使用下列Java在我的Android應用程序(發送通過SSL連接):

  DefaultHttpClient httpclient = new DefaultHttpClient(); 

      HttpPost httppost = new HttpPost("https://mysite/api/login"); 
      List<NameValuePair> nameValue = new ArrayList<NameValuePair>(); 
      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 

我沒有想到的是如何在android上接收身份驗證Cookie,並將每個請求發回給具有[Authorize]屬性的控制器。我對此很新,所以請原諒我的無知!

回答

相關問題