-1

如何使用asp.net中的登录控制和成员资格来实现让我保持登录asp.net

4

2 回答 2

1

通过添加一个复选框..

如果它被选中,你必须创建一个带有身份验证的cookie,如果它没有被选中,你必须把它放在会话中

http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.aspx

另一种方法是实现一个不持久的 cookie,如果它像这样未选中:

int timeout = rememberMe ? 525600 : 30; // Timeout in minutes, 525600 = 365 days.
var ticket = new FormsAuthenticationTicket(userName, rememberMe, timeout);
string encrypted = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
cookie.Expires = System.DateTime.Now.AddMinutes(timeout);
cookie.HttpOnly = true; // cookie not available in javascript.
Response.Cookies.Add(cookie);
于 2013-03-28T07:08:19.473 回答
0

首先,您需要在登录按钮上创建 Cookie,单击如下并将登录详细信息存储在其中

protected void btnLogin_Click(object sender, System.EventArgs e)
{
    string username = txtUsername.Text;
    string Password = txtPassword.Text;

    // Create Cookie and Store the Login Detail in it if check box is checked
    if ((CheckBox1.Checked == true)) {
        HttpCookie mycookie = new HttpCookie("LoginDetail");
        mycookie.Values("Username") = txtUsername.Text.Trim();
        mycookie.Values("Password") = txtPassword.Text.Trim();
        mycookie.Expires = System.DateTime.Now.AddDays(1);

        Response.Cookies.Add(mycookie);
    }    
    Response.Redirect("Default2.aspx");
}

然后检查cookie是否存在(记住我检查过),如果是,请填写以下详细信息-

protected void Page_Load(object sender, System.EventArgs e)
{
    //check if cookie exist then login page from 
    if ((Response.Cookies("LoginDetail") != null)) {    
        //Username 
        string uname =                     Response.Cookies("LoginDetail").Values("Username").ToString();
        string pass = Response.Cookies("LoginDetail").Values("Username").ToString();


    Response.Redirect("Default2.aspx");
    }

}
于 2013-03-28T09:34:49.447 回答