ASP.NET 身份验证“记住我”复选框未保持选中状态
我的登录页面出现问题。它从 cookie 中正确提取用户名,但是当我查看页面时,“记住我”的复选框不会被选中,即使在 Page_Load 上设置它的代码正在执行。
用于设置 cookie 的 LoggedIn 事件
protected void lLogin_LoggedIn(object sender, EventArgs e)
{
// If Remember me then set an appropriate cookie
if (lLogin.RememberMeSet)
{
HttpCookie loginCookie = new HttpCookie("loginCookie");
Response.Cookies.Remove("loginCookie");
Response.Cookies.Add(loginCookie);
loginCookie.Values.Add("username", lLogin.UserName.ToString());
DateTime dtExpiry = DateTime.Now.AddDays(15);
Response.Cookies["loginCookie"].Expires = dtExpiry;
}
// Set a cookie to expire after 1 second
else
{
HttpCookie loginCookie = new HttpCookie("loginCookie");
Response.Cookies.Remove("loginCookie");
Response.Cookies.Add(loginCookie);
loginCookie.Values.Add("username", lLogin.UserName.ToString());
DateTime dtExpiry = DateTime.Now.AddSeconds(1); //you can add years and months too here
Response.Cookies["loginCookie"].Expires = dtExpiry;
}
}
用于登录页面的 Page_Load 事件
protected void Page_Load(object sender, EventArgs e)
{
// Get username field to set focus
TextBox txtUserName = (TextBox)lLogin.FindControl("UserName");
if (!IsPostBack)
{
// For resetting the login url so that it doesn't have a return value in the URL
if (Request.QueryString["ReturnURL"] != null)
{
Response.Redirect("~/Login.aspx", true);
}
if (Request.IsAuthenticated)
{
Response.Redirect("~/Main/Home.aspx", true);
}
// If login cookie exists pull username
if (Request.Cookies["loginCookie"] != null)
{
HttpCookie loginCookie = Request.Cookies["loginCookie"];
lLogin.UserName = loginCookie.Values["username"].ToString();
CheckBox cb = (CheckBox)lLogin.FindControl("RememberMe");
// This is being Executed which is why I am puzzled
cb.Checked = true;
}
}
this.SetFocus(txtUserName);
}
我的 Web.Config 包含以下信息以及 MachineKey,这是正确的吗?
<authentication mode="Forms">
<forms loginUrl="Login.aspx" protection="All" timeout="60000" name="HRKCO" slidingExpiration="true" />
</authentication>
<sessionState mode="InProc" cookieless="UseCookies" timeout="30"/>
编辑
我通过使用解决了这个问题:
lLogin.RememberMeSet = true;
我认为这与查找 RememberMe CheckBox 并设置选中状态相同,但显然事实并非如此。只是想如果其他人也遇到类似的问题,我会分享这个。
I am having problems with my login page. It pulls through the Username correctly from the cookie, however the CheckBox for Remember Me does not become checked when I view the page, even though the code for setting it on the Page_Load is being executed.
LoggedIn event for setting the cookies
protected void lLogin_LoggedIn(object sender, EventArgs e)
{
// If Remember me then set an appropriate cookie
if (lLogin.RememberMeSet)
{
HttpCookie loginCookie = new HttpCookie("loginCookie");
Response.Cookies.Remove("loginCookie");
Response.Cookies.Add(loginCookie);
loginCookie.Values.Add("username", lLogin.UserName.ToString());
DateTime dtExpiry = DateTime.Now.AddDays(15);
Response.Cookies["loginCookie"].Expires = dtExpiry;
}
// Set a cookie to expire after 1 second
else
{
HttpCookie loginCookie = new HttpCookie("loginCookie");
Response.Cookies.Remove("loginCookie");
Response.Cookies.Add(loginCookie);
loginCookie.Values.Add("username", lLogin.UserName.ToString());
DateTime dtExpiry = DateTime.Now.AddSeconds(1); //you can add years and months too here
Response.Cookies["loginCookie"].Expires = dtExpiry;
}
}
Page_Load event for Login page
protected void Page_Load(object sender, EventArgs e)
{
// Get username field to set focus
TextBox txtUserName = (TextBox)lLogin.FindControl("UserName");
if (!IsPostBack)
{
// For resetting the login url so that it doesn't have a return value in the URL
if (Request.QueryString["ReturnURL"] != null)
{
Response.Redirect("~/Login.aspx", true);
}
if (Request.IsAuthenticated)
{
Response.Redirect("~/Main/Home.aspx", true);
}
// If login cookie exists pull username
if (Request.Cookies["loginCookie"] != null)
{
HttpCookie loginCookie = Request.Cookies["loginCookie"];
lLogin.UserName = loginCookie.Values["username"].ToString();
CheckBox cb = (CheckBox)lLogin.FindControl("RememberMe");
// This is being Executed which is why I am puzzled
cb.Checked = true;
}
}
this.SetFocus(txtUserName);
}
My Web.Config contains the following information as well as a MachineKey, Is this correct?
<authentication mode="Forms">
<forms loginUrl="Login.aspx" protection="All" timeout="60000" name="HRKCO" slidingExpiration="true" />
</authentication>
<sessionState mode="InProc" cookieless="UseCookies" timeout="30"/>
EDIT
I solved this problem by using:
lLogin.RememberMeSet = true;
I assumed this would be the same as finding the RememberMe CheckBox and setting the checked state but obviously it was not. Just thought I would share this if anyone else was having similar problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
web.config
的forms
元素中,您是否尝试过cookieless="UseCookies"
属性?我看到您有sessionState
使用它,但我相信您也需要它用于forms
。In the
forms
element of yourweb.config
, have you tried thecookieless="UseCookies"
attribute? I see that you have it forsessionState
, but I believe you need it forforms
too.我的问题的解决方案是我在
Page_Load
事件中设置复选框的方式。解决方案
The Solution to my problem was the way in which I was setting the checkbox in the
Page_Load
Event.Solution
将 RememberMeSet="true" 设置为 asp:Login 控件
Set RememberMeSet="true" to asp:Login control