我刚刚设置了一个代理页面来处理ajax请求但我无法让它工作,因为cookie根本没有得到保存。我的代码如下:
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");
}
}
}
登录请求应在客户端创建一些cookie,用于下一个请求(IsAuthenticated)和最后一个(实际的实际请求)。
但是IsAuthenticated在我正确登录后才返回false(我可以看到它按预期返回true)。就像我从未登录过
所以问题是:如何在代理中保存cookie?
我愿意接受考虑HttpHandlers
或其他理论的答案来做ajax代理,不一定是Aspx。
注意:如果我制作相同的请求系列,我可以看到cookie被创建,所以它必须是我的aspx代理的问题
答案 0 :(得分:1)
在这种情况下,cookie不会保存在客户端中,因为这是发出请求的服务器代码。 Cookie将作为响应的一部分发送到您的服务器,但不会发送回客户端 我想要获得您希望的行为,您需要从初始响应中获取cookie集合对象,并将其复制到以下两个请求对象中。
答案 1 :(得分: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");
}
}
}
再见!