【发布时间】:2010-09-07 15:22:54
【问题描述】:
我的登录页面有问题。它正确地从 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 并设置选中状态相同,但显然不是。只是想如果其他人有类似的问题,我会分享这个。
【问题讨论】:
-
不检查?还是没有正确获取校验值?
-
lLogin 是登录控件。它没有保持检查 - 即使 cb.CChecked = true :(
标签: asp.net forms-authentication