1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
public class HomeController : Controller { public ActionResult Index() { ViewBag.Title = "Home Page"; return View(); } [AllowAnonymous] public ActionResult Login() { if (User.Identity != null && User.Identity.IsAuthenticated) { this.clearUserState(); } return View(); } [HttpPost,ValidateAntiForgeryToken] public ActionResult Login(LoginFormData form) { if (form == null) throw new AuthenticationException("empty login form"); if (string.IsNullOrWhiteSpace(form.username)) throw new AuthenticationException($"empty {nameof(form.username)}"); if (string.IsNullOrWhiteSpace(form.password)) throw new AuthenticationException($"empty {nameof(form.password)}"); if (form.ts == 0) throw new AuthenticationException($"empty {nameof(form.ts)}(timestamp)"); if (!System.Text.RegularExpressions.Regex.Match(form.username, @"^[a-zA-Z0-9\-\.]{4,16}$").Success) throw new AuthenticationException($"invalid {nameof(form.username)}"); if (!( System.Text.RegularExpressions.Regex.Match(form.password, @"^[\s!-~]{10,32}$").Success && System.Text.RegularExpressions.Regex.Match(form.password, @"^(?=.*[a-z]+)(?=.*[A-Z]+)(?=.*[0-9]+)").Success )) throw new AuthenticationException($"invalid {nameof(form.password)}"); long unixTime = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds(); if (Math.Abs(unixTime - (form.ts) / 1000) > 10) throw new AuthenticationException("NO Form Replay Attach!!!");
using (var pCtx = new PrincipalContext(ContextType.Domain, Constants.ADDomain)) { bool flag = pCtx.ValidateCredentials(form.username, form.password); if (flag) { var ticket = new FormsAuthenticationTicket ( 1, form.username, DateTime.Now, DateTime.Now.AddMinutes(Constants.SessionDurationInMinutes), Constants.CreatePersistentCookie, "salt", FormsAuthentication.FormsCookiePath ); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket)) { Secure = true, HttpOnly = true, Path = ticket.CookiePath }; Response.Cookies.Add(cookie); Biz.Helper.UACHelper.UserLogin(cookie.Value); Logger.Info("Audit Success", form.username, this.Request?.UserHostAddress); return Redirect(FormsAuthentication.DefaultUrl); } else { Logger.Info("Audit Failure", form.username, this.Request?.UserHostAddress); return RedirectToAction("Login"); } } } [AllowAnonymous] public ActionResult Logout() { this.clearUserState(); Logger.Info("logout audit", this.User?.Identity?.Name, this.Request?.UserHostAddress); return View(); } private void clearUserState() { FormsAuthentication.SignOut(); HttpCookie authTokenCookie = new HttpCookie(FormsAuthentication.FormsCookieName, string.Empty) { Secure = true, HttpOnly = true }; authTokenCookie.Expires = DateTime.Now.AddYears(-1); Response.Cookies.Add(authTokenCookie); Session.Clear(); Session.Abandon(); } }
|