MVC5----用户登陆及验证码
随便写写记录一下学习的过程
登陆
Models中添加添加
public class LoginViewModel { [Required(ErrorMessage = "*")] [Display(Name = "机构号")] public string UserName { get; set; } [Required(ErrorMessage = "*")] [DataType(DataType.Password)] [Display(Name = "密码")] public string PassWord { get; set; } [Required(ErrorMessage = "*")] [Display(Name = "验证码")] public string Codeimg { get; set; } public string ErrorMsg { get; set; } }
Views代码:
其中ErrorMsg我是为了显示错误信息的,其他好的方法还不知道。。。
@using (Html.BeginForm("Login", "Admin", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" })) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) <div class="login_mid_right"> <div class="login_mid_right_ul"> <div class="form-group"> @Html.LabelFor(m => m.UserName, new { @class = "col-md-3 control-label" }) <div class="col-md-8"> @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" }) </div>@Html.ValidationMessageFor(m => m.UserName) </div> <div class="form-group"> @Html.LabelFor(m => m.PassWord, new { @class = "col-md-3 control-label" }) <div class="col-md-8"> @Html.PasswordFor(m => m.PassWord, new { @class = "form-control" }) </div> @Html.ValidationMessageFor(m => m.PassWord) </div> <div class="form-group"> @Html.LabelFor(m => m.Codeimg, new { @class = "col-md-3 control-label" }) <div class="col-md-4"> @Html.TextBoxFor(m => m.Codeimg, new { @class = "form-control" }) </div> @Html.ValidationMessageFor(m => m.Codeimg) <img class="codeimg" title="看不清,点击刷新" alt="看不清,点击刷新" src="/Extensions/Codeimg.ashx" onclick="javascript:this.src=this.src+'?rnd=' + Math.random();" /> </div> <div class="form-group"> <div class="col-md-offset-3 col-md-9"> <input type="submit" value="登 录" class="btn-lg btn-default" /> </div> </div> <div class="form-group"> <div class="col-md-offset-3 col-md-9"> @Html.ValidationMessageFor(m => m.ErrorMsg) </div> </div> </div> </div> }
登陆的验证,在对应的Controllers中:
public class AdminController : Controller { private SimonDBContext db = new SimonDBContext(); // // GET: /Admin/ public ActionResult Index() { return View(); } public ActionResult Login() { return View(); } public ActionResult LoginOut() { Session.Clear(); Session.Abandon(); return RedirectToAction("Login", "Admin"); } [HttpPost] public ActionResult Login([Bind(Include = "UserName,PassWord,Codeimg")] LoginViewModel login, string returnUrl) { //return View(); if (ModelState.IsValid) { int i = 9; if (Session["checkCode"].ToString() != login.Codeimg.ToUpper()) { ModelState.AddModelError("ErrorMsg", "验证码不正确!"); } else { i = Authentication(login.UserName, Common.Helper.Encryption.SHA256(login.PassWord)); } if (i == 0) { //Cookie //HttpCookie cookie = new HttpCookie("User"); //cookie.Values.Add("UserName", login.UserName); //Response.Cookies.Add(cookie); //Session Session["userName"] = login.UserName; return RedirectToAction("Index", "Admin"); } else if (i == 1) { ModelState.AddModelError("ErrorMsg", "该用户已被禁用!"); } else { ModelState.AddModelError("ErrorMsg", "密码或用户名错误!"); } } return View("Login"); } /// <summary> /// 登陆验证 /// </summary> /// <param name="userName"></param> /// <param name="pass"></param> /// <returns> /// 0:登录成功 /// 1:该用户已被禁用 /// 9:密码或用户名错误 /// </returns> public int Authentication(string userName, string pass) { int res = 0; AdminManager am = db.AdminManager.SingleOrDefault(c => c.UserName == userName); if (am == null) { return 9; } if (am.Flag != "1") { return 1; } if (am.PassWord != pass) { return 9; } return res; } }
做好了登陆,在其他页面就需要添加验证是否登陆,添加UserAuthorizeAttribute
public class UserAuthorizeAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { if (httpContext == null) { throw new ArgumentNullException("httpContext"); } if (HttpContext.Current.Session["userName"] == null) { return false; } return true; } }
在需要验证的Controller上添加 [UserAuthorize]