@model UserProject.ViewModels.Account @{ ViewBag.Title = ; } @using (Html.BeginForm(, ,FormMethod.Post, , role = })) { @Html.AntiForgeryToken() <hr /> @Html.ValidationSummary( }) <div > @Html.LabelFor(m => m.Name, }) <div > @Html.TextBoxFor(m => m.Name, }) @Html.ValidationMessageFor(m => m.Name, }) </div> </div> <div > @Html.LabelFor(m => m.Password, }) <div > @Html.PasswordFor(m => m.Password, }) @Html.ValidationMessageFor(m => m.Password, }) </div> </div> <div > <div > <input type=value==/> </div> </div> }
登录的Action:
[AllowAnonymous] public ActionResult Login() { return View(); } [HttpPost, AllowAnonymous] public ActionResult Login(Account model) { if (ModelState.IsValid) { var user = db.Users.SingleOrDefault(t => t.UserName == model.Name && t.Password == model.Password); if (user != null) { FormsAuthentication.SetAuthCookie(model.Name, false);//将用户名放入Cookie中 ); } else { ModelState.AddModelError(, ); } } return View(model); } public ActionResult LogOff() { FormsAuthentication.SignOut(); ); }
按照以上方式 访问Details这个Action的时候必须是管理员角色。
此仅仅是测试项目,作为完成私活的部分知识点的总结。