Hai guys,
- what are the possible ways of implementing remember me option in an asp.net web application? alt text http://www.freeimagehosting.net/uploads/0f9ebba1bb.jpg
Hai guys,
如果您使用窗体身份验证,只是通过true
的第二个参数RedirectFromLoginPage 。
否则,想法基本上是一样的:你需要创建一个所谓的“ 持久性cookie ”,这意味着你必须指定正确的cookie的有效期。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["myCookie"] != null)
{
HttpCookie cookie = Request.Cookies.Get("myCookie");
txtUserName.Text = cookie.Values["username"];
txtPassword.Attributes.Add("value", cookie.Values["password"]);
}
}
}
protected void btnLogin_Click(object sender, EventArgs e)
{
bool IsRemember = chkRememberMe.Checked;
if (IsRemember)
{
myCookie.Values.Add("username", txtUserName.Text);
myCookie.Values.Add("password", txtPassword.Text);
myCookie.Expires = DateTime.Now.AddDays(15);
}
else
{
myCookie.Values.Add("username", string.Empty);
myCookie.Values.Add("password", string.Empty);
myCookie.Expires = DateTime.Now.AddMinutes(5);
}
Response.Cookies.Add(myCookie);
}
看看这里: 如何创建一个ASP.NET登录页面
<asp:Login ID="Login1"
runat="server"
DestinationPageUrl="~/MembersHome.aspx">
</asp:Login>