如何实现让我登录asp.net

时间:2013-03-28 07:02:54

标签: asp.net

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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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");
    }

}