2012-02-20 34 views
0

我剛剛設置了一個代理頁面來處理ajax請求,但我不能得到它的工作,因爲cookie根本沒有得到保存。我的代碼如下:Cookie不保存與aspx代理

public partial class JsonProxy : System.Web.UI.Page 
{ 

private string username; 
private string password; 
private int idPlant; 
private string mode; 

protected void Page_Load(object sender, EventArgs e) 
{ 
    try 
    { 
     username = !String.IsNullOrEmpty(HttpUtility.UrlDecode(Request.Form["username"])) ? HttpUtility.UrlDecode(Request.Form["username"].ToString()) : string.Empty; 
     password = !String.IsNullOrEmpty(HttpUtility.UrlDecode(Request.Form["password"])) ? HttpUtility.UrlDecode(Request.Form["password"].ToString()) : string.Empty; 
     idPlant = !String.IsNullOrEmpty(HttpUtility.UrlDecode(Request.Form["idPlant"])) ? int.Parse(HttpUtility.UrlDecode(Request.Form["idPlant"].ToString())) : 0; 
     mode = !String.IsNullOrEmpty(HttpUtility.UrlDecode(Request.Form["mode"])) ? HttpUtility.UrlDecode(Request.Form["mode"].ToString()) : string.Empty; 

     string response = ""; 
     HttpWebRequest wc; 

     if (!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password) && idPlant != 0 && !String.IsNullOrEmpty(mode)) 
     { 
      //First do authentication 
      wc= (HttpWebRequest)WebRequest.Create("http://10.255.255.10/Base/Authentication/Login/" + username + "/" + password + ".aspx"); 
      wc.Method = "GET"; 

      StreamReader reader = new StreamReader(((HttpWebResponse)wc.GetResponse()).GetResponseStream()); 
      if (reader.ReadToEnd().Contains("true")) 
      { 
       //Then check that authentication succeded 
       wc = (HttpWebRequest)WebRequest.Create("http://10.255.255.10/Base/Authentication/IsAuthenticated.aspx"); 
       wc.Method = "GET"; 

       reader = new StreamReader(((HttpWebResponse)wc.GetResponse()).GetResponseStream()); 

       string str = reader.ReadToEnd(); 
       if (str.Contains("true")) 
       { 
        //Then make BP request 
        string methodName = "/Base/BusinessPlan/GetBPAlll/" + idPlant + ".aspx"; 
        wc = (HttpWebRequest)WebRequest.Create("http://10.255.255.10" + methodName); 
        wc.Method = "GET"; 
        reader = new StreamReader(((HttpWebResponse)wc.GetResponse()).GetResponseStream()); 
        response = reader.ReadToEnd(); 
       } 
      } 

     } 

     //Last: write response 
     Response.ContentType = "application/json"; 
     Response.Write(response); 
    } 
    catch (WebException ex) 
    { 
     Response.Write("error"); 
    } 
} 

} 

的登錄請求應該在將在下一個請求(IsAuthenticated),並在最後一個(其實真正的要求)使用的客戶端創建一些餅乾。 然而,IsAuthenticated只是在我正確登錄後才返回false(我可以看到它按預期返回true)。這就像是我從未登錄過。
所以問題是:我如何將Cookie保存在代理中?
我願意考慮也考慮HttpHandlers或其他理由來做ajax代理的答案,不一定是Aspx。
注:如果我提出同樣的要求系列,我可以看到的cookie獲得創建因此它必定是一個關於我的aspx代理

回答

1

在這種情況下的cookie不會被保存在客戶端,因爲這件事是讓服務器代碼請求。 Cookie將作爲響應的一部分發送給您的服務器,但不會返回給客戶端。
我認爲要獲得您期望的行爲將需要從初始響應中獲取cookie集合對象,並將其複製到以下兩個請求對象中。

+0

是的,這是我做的。我接受了你的答案,即使我會給出完整的答案以防萬一有人遇到同樣的問題。 – andreapier 2012-02-20 16:50:19

0

經過進一步搜索,我發現你需要手動將cookiesContainer傳遞給下一個請求者。這裏是完整的,工作示例:

public partial class JsonProxy : System.Web.UI.Page 

{

private string username; 
private string password; 
private int idPlant; 
private string mode; 
private CookieContainer cookieJar = new CookieContainer(); 

protected void Page_Load(object sender, EventArgs e) 
{ 
    try 
    { 
     username = !String.IsNullOrEmpty(HttpUtility.UrlDecode(Request.Form["username"])) ? HttpUtility.UrlDecode(Request.Form["username"].ToString()) : string.Empty; 
     password = !String.IsNullOrEmpty(HttpUtility.UrlDecode(Request.Form["password"])) ? HttpUtility.UrlDecode(Request.Form["password"].ToString()) : string.Empty; 
     idPlant = !String.IsNullOrEmpty(HttpUtility.UrlDecode(Request.Form["idPlant"])) ? int.Parse(HttpUtility.UrlDecode(Request.Form["idPlant"].ToString())) : 0; 
     mode = !String.IsNullOrEmpty(HttpUtility.UrlDecode(Request.Form["mode"])) ? HttpUtility.UrlDecode(Request.Form["mode"].ToString()) : string.Empty; 

     string response = ""; 
     HttpWebRequest wc; 

     if (!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password) && idPlant != 0 && !String.IsNullOrEmpty(mode)) 
     { 
      //First do authentication 
      wc= (HttpWebRequest)WebRequest.Create("http://10.255.255.10/Base/Authentication/Login/" + username + "/" + password + ".aspx"); 
      wc.Method = "GET"; 
      wc.CookieContainer = cookieJar; 

      StreamReader reader = new StreamReader(((HttpWebResponse)wc.GetResponse()).GetResponseStream()); 
      if (reader.ReadToEnd().Contains("true")) 
      { 
       //Then check that authentication succeded 
       wc = (HttpWebRequest)WebRequest.Create("http://10.255.255.10/Base/Authentication/IsAuthenticated.aspx"); 
       wc.Method = "GET"; 
       wc.CookieContainer = cookieJar; 

       reader = new StreamReader(((HttpWebResponse)wc.GetResponse()).GetResponseStream()); 

       string str = reader.ReadToEnd(); 
       if (str.Contains("true")) 
       { 
        //Then make BP request 
        string methodName = "/Base/BusinessPlan/GetBPAll/" + idPlant + ".aspx"; 
        wc = (HttpWebRequest)WebRequest.Create("http://10.255.255.10" + methodName); 
        wc.Method = "GET"; 
        wc.CookieContainer = cookieJar; 
        reader = new StreamReader(((HttpWebResponse)wc.GetResponse()).GetResponseStream()); 
        response = reader.ReadToEnd(); 
       } 
      } 
     } 

     //Last: write response 
     Response.ContentType = "application/json"; 
     Response.Write(response); 
    } 
    catch (WebException ex) 
    { 
     Response.ContentType = "application/json"; 
     Response.Write("error"); 
    } 
} 

}

再見!