Asp.net 未使用aspx代理保存Cookie
我刚刚设置了一个代理页面来处理ajax请求,但我无法让它工作,因为cookies根本无法保存。我的代码如下:Asp.net 未使用aspx代理保存Cookie,asp.net,ajax,proxy,Asp.net,Ajax,Proxy,我刚刚设置了一个代理页面来处理ajax请求,但我无法让它工作,因为cookies根本无法保存。我的代码如下: 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, EventAr
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,这些cookie将在下一个请求(已验证)和上一个请求(实际请求)中使用。
但是,在我正确登录后,IsAuthenticated返回false(我可以看到它会像预期的那样返回true)。就像我从未登录过一样。所以问题是:如何在代理中保存cookies?
我愿意接受的答案还考虑了
HttpHandlers
或其他进行ajax代理的技巧,不一定是Aspx。注意:如果我发出相同的请求序列,我可以看到创建了cookie,因此这一定是关于我的aspx代理的问题。在这种情况下,cookie不会保存在客户端中,因为发出请求的是服务器代码。Cookie将作为响应的一部分发送到服务器,但不会返回到客户端。
我认为要获得您希望的行为,您需要从初始响应中获取cookie收集对象,并将其复制到以下两个请求对象中。进一步搜索后,我发现您需要手动将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");
}
}