Cookie 未使用 aspx 代理保存

发布于 2025-01-07 12:38:49 字数 2965 浏览 1 评论 0原文

我刚刚设置了一个代理页面来处理 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,这些cookie用于下一个请求(IsAuthenticated)和最后一个请求(实际上是真正的请求)。 然而,在我正确登录后,IsAuthenticated 返回 false(我可以看到它按预期返回 true)。就像我从未登录过一样。
所以问题是:如何在代理中保存 cookie?
我愿意接受考虑 HttpHandlers 或其他进行 ajax 代理的技术的答案,不一定是 Aspx。
注意:如果我发出相同的请求系列,我可以看到 cookies 被创建,所以这一定是我的 aspx 代理的问题

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自找没趣 2025-01-14 12:38:49

在这种情况下,cookie 不会保存在客户端中,因为这是发出请求的服务器代码。 cookie 将作为响应的一部分发送到您的服务器,但不会返回到客户端。
我认为要获得您希望的行为,您需要从初始响应中获取 cookie 集合对象,并将其复制到以下两个请求对象中。

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.

与他有关 2025-01-14 12:38:49

经过进一步搜索,我发现您需要手动将 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");
    }
}

}

再见!

After further searching i found that you need to manually pass the cookiesContainer to next requestes. here is the complete and working example:

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");
    }
}

}

Bye!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文