2013-12-15 27 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显示此部分的错误 –