在一个asp.net web应用程序与rememberMe选项(RememberMe option

2019-10-19 04:38发布

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

Answer 1:

如果您使用窗体身份验证,只是通过true的第二个参数RedirectFromLoginPage 。

否则,想法基本上是一样的:你需要创建一个所谓的“ 持久性cookie ”,这意味着你必须指定正确的cookie的有效期。



Answer 2:

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);
}


Answer 3:

看看这里: 如何创建一个ASP.NET登录页面

<asp:Login ID="Login1" 
           runat="server" 
           DestinationPageUrl="~/MembersHome.aspx">
</asp:Login>


文章来源: RememberMe option in an asp.net web application