2013-04-26 108 views
0

我成功登錄到MVC 4 Web API網站並獲得一個。 ASPXAUTH的Set-Cookie是這樣的:WebAPI和表單身份驗證CookieContainer

private void Login() 
    { 
     var client = new RestClient("http://QQQQ/MobileAPI"); 
     client.CookieContainer = new CookieContainer(); 
     var request = new RestRequest {Resource = "/api/account/Logon", Method = Method.POST}; 
     request.AddObject(new {Username = "john", Password = "secret"}); 

     var responseLogin = client.Execute(request); 
     if (responseLogin.StatusCode == HttpStatusCode.OK) 
      ProcessData(client.CookieContainer); 
    } 

正如你可以看到我通過的CookieContainerProcessData()功能,然後處理的RESTful命令,坐在後面的[AUTHORIZE]控制器。

的過程數據()調用失敗並HttpStatusCode.Unauthorized

問:我認爲我沒有正確發送的驗證?

private void ProcessData(CookieContainer cookieJar) 
    { 
     var client = new RestClient("QQQQQ/MobileAPI"); 
     client.CookieContainer = cookieJar; 
     var request = new RestRequest {Resource = "/api/values/5", Method = Method.GET}; 

     var responseForUser = client.ExecuteAsGet<AppUser>(request, "GET"); 

     if (responseForUser.StatusCode == HttpStatusCode.OK) 
     { 
      AppUser app = responseForUser.Data; 
      Console.WriteLine(app.CUSAName); 
     } 

     if (responseForUser.StatusCode == HttpStatusCode.Unauthorized) 
     { 
      Console.WriteLine("FAILED"); 
     } 
    } 
+0

你的第二個電話是否發送cookie? – leastprivilege 2013-04-27 06:59:09

+0

API控制器的工作方式與Http Controller不同,我懷疑授權屬性可以與API控制器一起使用。您將必須檢查API控制器的生命週期。 – 2013-04-27 07:06:21

回答

0

原來我剛剛到服務器IISRESET復位。它可能處於一個糟糕的狀態。

一旦我這樣做,上述代碼在Android設備上完美工作