【问题标题】:Remember me in Asp.Net Mvc4在 Asp.Net Mvc4 中记住我
【发布时间】:2014-03-06 17:12:43
【问题描述】:

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

这是我的模型代码:

 @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>
                }

这就是我在 Controller 中获取值的方式。 这是我的控制器代码:

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

【问题讨论】:

    标签: asp.net-mvc-4 razor entity-framework-4


    【解决方案1】:

    您可以使用以下代码实现此目的

     //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);
    

    【讨论】:

    • 我使用了相同的代码,但它显示错误。错误消息是:FormsAuthenticationTicket 不包含采用 4 个参数的构造函数。
    猜你喜欢
    • 2011-01-28
    • 2011-03-22
    • 2017-06-04
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 2013-08-07
    • 2011-08-02
    相关资源
    最近更新 更多