簡體   English   中英

登錄表單中的“記住我”功能

[英]“Remember me” functionality in a Login form

我正在使用linq進行實體連接。 我想讓用戶一旦輸入帳戶便保持登錄狀態,這是我的代碼。 沒用 請幫助

    if (this.ChkRememberme != null && this.ChkRememberme.Checked == true)
    {
        HttpCookie cookie = new HttpCookie(TxtUserName.Text, TxtPassword.Text);
        cookie.Expires.AddYears(1);
        Response.Cookies.Add(cookie);
    }
 if (this.ChkRememberme != null && this.ChkRememberme.Checked == true)
 {
    int timeout = rememberMe ? 525600 : 30; // Timeout in minutes, 525600 = 365 days.
    var ticket = new FormsAuthenticationTicket(TxtUserName.Text, TxtPassword.Text);
    string encrypted = FormsAuthentication.Encrypt(ticket);
    var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
    cookie.Expires = System.DateTime.Now.AddMinutes(timeout);// Not my line
    cookie.HttpOnly = true; // cookie not available in javascript.
    Response.Cookies.Add(cookie);
}

轉到您的web.config並找到身份驗證元素。 您可以在此處設置Cookie的過期時間(以分鍾為單位),如下所示:

<system.web>
    <authentication mode="Forms">
        <forms loginUrl="~/Account/Login" 
               name="myCookie"                  <!-- optional, if you want to rename it -->
               timeout="2880" />                <!-- expires in 48 hours -->
    </authentication>
</system.web>

來源: 如何在C#中應用“記住我”

希望這可以幫助

快樂編碼..!

我建議在您的應用程序中使用MembershipReboot進行身份驗證(包括示例)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM