i have just setup a proxy page to handle ajax requests but i cannot get it working as cookies doesn’t get saved at all. My code is as follows:
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");
}
}
}
The login request should create some cookies in the client that are used in the next request (IsAuthenticated) and in the last one (the real request actually).
However IsAuthenticated return false just after i did the login correctly (i can see it return true as expected). It is like if i never logged in.
So the question is: how can i save cookies in proxy?
I am open to answers that take in consideration also HttpHandlers or other thechiques to do ajax proxying, not necessarily Aspx.
Note: if i make the same request series i can see cookies get created so it must be a matter about my aspx proxy
In this case cookies will not be saved in the client as this is server code that is making the request. The cookies will be sent as part of the response to your server but not back to the client.
I think to get the behaviour you are hoping for you will need to get the cookie collection object from the initial response and copy that into the following two request objects.