2014-03-06 34 views
0

我是一个新的Asp.Net Mvc4与实体框架。我正在尝试在登录页面中实现记住我的任务。在这里,我没有使用任何模型属性记住我。我只是将模型的值传递给控制器​​。请帮助我在控制器中设置一个cookie。提前感谢。记住我在Asp.Net Mvc4

这是我的标准守则:

@using (Html.BeginForm("LogIn", "Login")) 
       { 

        <p class="mt5 mb20">Login to access your account.</p>*@ 
        <div> 
        @Html.ValidationSummary(true) 
        </div> 
        <div> 
        @Html.TextBoxFor(u => u.UserName, new { @class = "form-control uname", placeholder = "UserName" }) 

        @Html.PasswordFor(u => u.UserPassword, new { @class = "form-control pword", placeholder = "Password" }) 



        @Html.ActionLink("Forgot Password?","Forgotpassword","Login",new {@class="navbar-link"}) 
        <span>Remember</span><input type="checkbox" name="remember"/> 
        <button class="btn btn-success btn-block" value="login">Sign In</button> 
        </div> 
       } 

这是我收到的控制器价值的方式。 这是我的控制器代码:

[HttpPost] 
     public ActionResult LogIn(Tbl_Users user, FormCollection forms) 

回答

0

您可以使用下面的代码

//create the authentication ticket 
    var authTicket = new FormsAuthenticationTicket(
     1, 
     userId, //user id 
     DateTime.Now, 
     DateTime.Now.AddMinutes(2000), // expiry in minutes 
     rememberMe, //true to remember if it is checked 
     "", //roles 
     "/" 
    ); 

    //encrypt the ticket and add it to a cookie 
    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket)); 
    Response.Cookies.Add(cookie); 
+0

我使用相同的代码实现这一点,但它显示错误。错误消息是:FormsAuthenticationTicket不包含具有4个参数的构造函数。 – Vetri