如何检查浏览器是否在网络应用中启用了Cookie?

时间:2012-03-06 13:10:20

标签: c# asp.net internet-explorer firefox cookies

我决定重新编写问题,因为我找到了一些关于如何完成上述操作的信息: http://msdn.microsoft.com/en-us/library/aa289495%28v=vs.71%29.aspx

但是,我的问题是,我正在尝试Page_Load Login.aspx.cs Login.aspx.cs事件中的所有内容。

最初看起来工作正常,直到我尝试通过输入我的凭据并单击“登录”按钮来实际登录。然后所有的地狱失败,我得到一个无限循环。它始终在TestForCookies.aspx.cs"?AcceptCookies=1上的Page_Loads之间来回切换。每次重定向网址都会增长另一个protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (Request.QueryString["AcceptsCookies"] == null) { Response.Cookies["TestCookie"].Value = "ok"; Response.Cookies["TestCookie"].Expires = DateTime.Now.AddMinutes(1); Response.Redirect(BasePage.ResolveUrl("~/Common/TestForCookies.aspx?redirect=" + Server.UrlEncode(Request.Url.ToString()))); } else { LoginBox.InstructionText = "Accept cookies = " + Request.QueryString["AcceptsCookies"]; } } } “。是否有解决方法?

Login.aspx.cs代码:

protected void Page_Load(object sender, EventArgs e)
{
    string redirect = Request.QueryString["redirect"];

    string acceptsCookies = null;
    // Was the cookie accepted?
    if (Request.Cookies["TestCookie"] == null)
    {
        // No cookie, so it must not have been accepted
        acceptsCookies = "0";
    }
    else
    {
        acceptsCookies = "1";
        // Delete test cookie
        Response.Cookies["TestCookie"].Expires = DateTime.Now.AddDays(-1);
    }

    string url = redirect + "?AcceptsCookies=" + acceptsCookies;


    Response.Redirect(url);
}

TestForCookies.aspx.cs代码:

{{1}}

2 个答案:

答案 0 :(得分:2)

这可能很毛茸茸。我在登录页面上所做的基本上是以下步骤:

  1. 在“登录”上单击回发到服务器,验证用户的凭据。如果登录成功......
  2. 设置我的cookie并发送JS以点击隐藏的“确认cookie”按钮返回浏览器。这是一次额外的往返,但cookie应该是有效载荷的一部分。
  3. 在“确认cookie”按钮的处理程序中返回服务器,我检查cookie。如果它在那里,我们很好,我重定向。如果没有,我会发回内容,说明已禁用cookie,并附带有关如何启用它们的链接。
  4. 希望这有帮助!

答案 1 :(得分:0)

我想我解决了。由于URL地址格式错误,导致重定向无法正常完成。

Login.aspx.cs

    if (!Page.IsPostBack)
    {
        if (Request.QueryString["AcceptsCookies"] != null)
        {
            if (Request.QueryString["AcceptsCookies"].ToString() == "0")
            {
                LoginBox.InstructionText = Resources.Resource.Login10;
            }
        }
        else
        {
            Response.Cookies["TestCookie"].Value = "ok";
            Response.Cookies["TestCookie"].Expires = DateTime.Now.AddMinutes(1);

            string url = Request.Url.ToString();
            url = url.Replace("ReturnUrl", "AcceptsCookies=&ReturnUrl");

            Response.Redirect(BasePage.ResolveUrl("~/Common/TestForCookies.aspx?redirect=" + Server.UrlEncode(url)));
        }

    }

TestForCookies.aspx.cs

    string redirect = Request.QueryString["redirect"];

    string acceptsCookies = null;
    // Was the cookie accepted?
    if (Request.Cookies["TestCookie"] == null)
    {
        // No cookie, so it must not have been accepted
        acceptsCookies = "0";
    }
    else
    {
        acceptsCookies = "1";
        // Delete test cookie
        Response.Cookies["TestCookie"].Expires = DateTime.Now.AddDays(-1);
    }

    redirect = redirect.Replace("AcceptsCookies=", "AcceptsCookies=" + acceptsCookies);

    Response.Redirect(BasePage.ResolveUrl(redirect), true);
}