public HttpResponseMessage GetCaptcha(HttpRequestMessage request) { var randomText = TextHelper.GenerateRandomText(4); var rnd = new Random(); var fonts = new[] { "Verdana", "Times New Roman" }; float orientationAngle = rnd.Next(0, 359); var index0 = rnd.Next(0, fonts.Length); var familyName = fonts[index0]; using (var bmpOut = new Bitmap(width, height)) { var g = Graphics.FromImage(bmpOut); var gradientBrush = new LinearGradientBrush(new Rectangle(0, 0, width, height), Color.White, Color.DarkGray, orientationAngle); g.FillRectangle(gradientBrush, 0, 0, width, height); DrawRandomLines(ref g, width, height); g.DrawString(randomText, new Font(familyName, 18), new SolidBrush(Color.LightSlateGray), 0, 2); var ms = new MemoryStream(); bmpOut.Save(ms, ImageFormat.Png); var bmpBytes = ms.GetBuffer(); bmpOut.Dispose(); ms.Close(); return(request.CreateResponse(HttpStatusCode.OK, new CaptchaModel { Hash = Compute.ComputeMd5Hash(randomText + GetSalt()), ImageByteArray = bmpBytes })); } }
public ActionResult Login(string returnUrl) { string randomText = string.Empty; var bmpBytes = GetCatcha(out randomText); var hash = Compute.ComputeMd5Hash(randomText + GetSalt()); Session["CaptchaHash"] = hash; var model = new LoginViewModel(bmpBytes, string.Empty, string.Empty, string.Empty); ViewBag.ReturnUrl = returnUrl; return(View(model)); }
public ActionResult Login(string userName, string password, string captcha, string returnUrl) { var hash = Compute.ComputeMd5Hash(captcha + GetSalt()); var captchaHash = Session["CaptchaHash"] as string; if (hash.Equals(captchaHash)) { ApplicationUser user = UserManager.Instance.UserManagerment.Find(userName, password); if (user != null) { if (!user.Status) { ViewData["ErrorLogin"] = "******"; } else { IAuthenticationManager authenticationManager = HttpContext.GetOwinContext().Authentication; authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); ClaimsIdentity identity = UserManager.Instance.UserManagerment.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationProperties props = new AuthenticationProperties(); authenticationManager.SignIn(props, identity); return(RedirectToLocal(returnUrl)); } } else { ViewData["ErrorLogin"] = "******"; } } else { ViewData["ErrorLogin"] = "******"; } string randomText = string.Empty; var bmpBytes = GetCatcha(out randomText); Session["CaptchaHash"] = Compute.ComputeMd5Hash(randomText + GetSalt()); var model = new LoginViewModel(bmpBytes, userName, password, captcha); ViewBag.ReturnUrl = returnUrl; return(View(model)); }
public HttpResponseMessage VatelidateCaptcha(HttpRequestMessage request, string hash, string captchaValue) { var tocheck = Compute.ComputeMd5Hash(captchaValue + GetSalt()); return(request.CreateResponse(HttpStatusCode.OK, hash.Equals(tocheck))); }