嘿伙计们,
- 在 asp.net Web 应用程序中实现记住我选项的可能方法是什么? 替代文字 http://www.freeimagehosting.net/uploads/0f9ebba1bb.jpg
嘿伙计们,
如果您使用表单身份验证,只需将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>