日期:2014-05-17 浏览次数:20554 次
@{ ViewBag.Title = "后台管理登录"; } @section Header{ <script type="text/javascript"> $(function () { $("#username").focus(); });</script> <style type="text/css"> input[type="text"], input[type="password"] { width: 160px; } </style> } <h2> 登录</h2> @Html.ValidationSummary("登录失败,无效的用户名或密码") @using (Html.BeginForm()) { <div style="width: 380px; margin: 0px auto"> <fieldset> <legend>帐号信息</legend> <p> <label for="username"> 帐号:</label> @Html.TextBox("username") @Html.ValidationMessage("username") </p> <p> <label for="password"> 密码:</label> @Html.Password("password") @Html.ValidationMessage("password") </p> <p> @Html.CheckBox("rememberMe") <label class="inline" for="rememberMe"> 下次自动登录(公共电脑上谨慎使用)</label> </p> <div class="center"> <input type="submit" value="登录(L)" accesskey="L" /> </div> <p>@Html.ActionLink("还没有账号?立即注册", "Register")</p> </fieldset> </div> }
public ActionResult Login() { return View(); } [HttpPost] public ActionResult Login(LogOnModel model, string returnUrl) { if (ModelState.IsValid)//??? { RegisteredUser usr = RegisteredUserBLL.AuthenticateUser(model.UserName, model.Password); if (usr != null) { FormsAuthentication.SetAuthCookie(model.UserName + "|" + usr.RealName + "|" + usr.UserType, model.RememberMe); if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) 写???? return Redirect(returnUrl); return RedirectToAction("Index", "Home"); } ModelState.AddModelError("", "登录账号或密码不正确"); } return View(model); }
public class LogOnModel { [Required] [Display(Name = "账号")] public string UserName { get; set; } [Required] [DataType(DataType.Password)] [Display(Name = "密码")] public string Password { get; set; } [Display(Name = "记住登录信息")] public bool RememberMe { get; set; } }