我是使用Entity Framework的Asp.Net Mvc4的新手。我正在尝试在登录页面中实现记住我的任务。在这里,我没有使用任何模型属性记住我。我只是将值从Model传递给Controller。请帮我在控制器中设置一个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)
答案 0 :(得分: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);