我决定重新编写问题,因为我找到了一些关于如何完成上述操作的信息: 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}}
答案 0 :(得分:2)
这可能很毛茸茸。我在登录页面上所做的基本上是以下步骤:
希望这有帮助!
答案 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);
}