2013-12-15 30 views
1

我正在使用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); 
    } 
+5

我不會把密碼放在cookie中使用另一個標記(只需用戶名就足夠了) - 看看這裏http://stackoverflow.com/questions/2452656/asp-net-mvc-rememberme –

+1

你可以使用另一個SO回答,像這樣: http://stackoverflow.com/questions/5619791/implementing-remember-me-feature-in-asp-net-mvc – idlerboris

+0

你能幫助我確切的代碼..? –

回答

0

我建議使用MembershipReboot在您的應用程序認證的目的(樣本包括在內)。

3
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> 

來源:how to apply "Remember Me" in c#

希望這有助於

編碼愉快.. !!

+0

我需要在web配置中寫些東西嗎??? –

+0

實施後是否收到任何錯誤? –

+0

FormsAuthenticationTicket和rememberMe顯示此部分的錯誤 –