public ActionResult GetCaptchaImage() { CreateAccountCacheItem createAccountCacheItem = GetCachedCreateAccountAttempt(false); if (createAccountCacheItem == null) { return(StatusCode(400)); } CaptchaImage ci = new CaptchaImage(createAccountCacheItem.CaptchaText, 240, 60, "Century Schoolbook"); try { // Write the image to the response stream in JPEG format. using (MemoryStream ms = new MemoryStream()) { ci.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); return(File(ms.ToArray(), "image/png")); } } catch (Exception err) { if (!err.Message.Contains("Specified method is not supported.")) { throw; } } finally { ci.Dispose(); } return(null); }
private void PrepareCreateAccountModel(ref CreateAccountViewModel model) { AddressOptions addressOptions = _accountProvider.GetAddressOptions(); model.ShowAddressLine1 = addressOptions.HasFlag(AddressOptions.AddressLine1Show); model.ShowAddressLine2 = addressOptions.HasFlag(AddressOptions.AddressLine2Show); model.ShowAddressLine3 = addressOptions.HasFlag(AddressOptions.AddressLine3Show); model.ShowBusinessName = addressOptions.HasFlag(AddressOptions.BusinessNameShow); model.ShowCity = addressOptions.HasFlag(AddressOptions.CityShow); model.ShowCounty = addressOptions.HasFlag(AddressOptions.CountyShow); model.ShowPostcode = addressOptions.HasFlag(AddressOptions.PostCodeShow); model.ShowTelephone = addressOptions.HasFlag(AddressOptions.TelephoneShow); model.ShowCaptchaImage = true; CreateAccountCacheItem createAccountCacheItem = GetCachedCreateAccountAttempt(true); createAccountCacheItem.CaptchaText = GetRandomWord(6, CaptchaCharacters); }
private CreateAccountCacheItem GetCachedCreateAccountAttempt(bool createIfNotExist) { CreateAccountCacheItem Result = null; string cacheId = GetIpAddress(); CacheItem loginCache = _createAccountCache.Get(cacheId); if (loginCache != null) { Result = (CreateAccountCacheItem)loginCache.Value; } else if (createIfNotExist && loginCache == null) { Result = new CreateAccountCacheItem(); loginCache = new CacheItem(cacheId, Result); _createAccountCache.Add(cacheId, loginCache); } return(Result); }
private void ValidateCreateAccountModel(ref CreateAccountViewModel model) { CreateAccountCacheItem createAccountCacheItem = GetCachedCreateAccountAttempt(true); if (!String.IsNullOrEmpty(createAccountCacheItem.CaptchaText)) { if (!createAccountCacheItem.CaptchaText.Equals(model.CaptchaText)) { ModelState.AddModelError(String.Empty, "Invalid Validation Code"); } } createAccountCacheItem.CreateAttempts++; if (createAccountCacheItem.CreateAttempts > 10) { ModelState.AddModelError(String.Empty, "Too many attempts, please wait 30 minutes and try again"); } AddressOptions addressOptions = _accountProvider.GetAddressOptions(); if (!ValidatePasswordComplexity(model.Password)) { ModelState.AddModelError(String.Empty, "Password does not match complexity rules."); } if (!model.Password.Equals(model.ConfirmPassword)) { ModelState.AddModelError("", "Confirm password must match Password"); } if (addressOptions.HasFlag(AddressOptions.AddressLine1Mandatory) && String.IsNullOrEmpty(model.AddressLine1)) { ModelState.AddModelError(nameof(model.AddressLine1), "Address line 1 is required"); } if (addressOptions.HasFlag(AddressOptions.AddressLine2Mandatory) && String.IsNullOrEmpty(model.AddressLine2)) { ModelState.AddModelError(nameof(model.AddressLine2), "Address line 2 is required"); } if (addressOptions.HasFlag(AddressOptions.AddressLine3Mandatory) && String.IsNullOrEmpty(model.AddressLine3)) { ModelState.AddModelError(nameof(model.AddressLine3), "Address line 3 is required"); } if (addressOptions.HasFlag(AddressOptions.CityMandatory) && String.IsNullOrEmpty(model.City)) { ModelState.AddModelError(nameof(model.City), "City is required"); } if (addressOptions.HasFlag(AddressOptions.CountyMandatory) && String.IsNullOrEmpty(model.County)) { ModelState.AddModelError(nameof(model.County), "County is required"); } if (addressOptions.HasFlag(AddressOptions.PostCodeMandatory) && String.IsNullOrEmpty(model.Postcode)) { ModelState.AddModelError(nameof(model.Postcode), "Postcode is required"); } if (addressOptions.HasFlag(AddressOptions.BusinessNameMandatory) && String.IsNullOrEmpty(model.BusinessName)) { ModelState.AddModelError(nameof(model.BusinessName), "Business Name is required"); } if (addressOptions.HasFlag(AddressOptions.TelephoneMandatory) && String.IsNullOrEmpty(model.Telephone)) { ModelState.AddModelError(nameof(model.Telephone), "Telephone is required"); } createAccountCacheItem.CaptchaText = GetRandomWord(6, CaptchaCharacters); model.CaptchaText = String.Empty; }
private void ValidateCreateAccountModel(ref CreateAccountViewModel model) { CreateAccountCacheItem createAccountCacheItem = GetCachedCreateAccountAttempt(true); if (!String.IsNullOrEmpty(createAccountCacheItem.CaptchaText)) { if (!createAccountCacheItem.CaptchaText.Equals(model.CaptchaText)) { ModelState.AddModelError(String.Empty, Languages.LanguageStrings.CodeNotValid); } } createAccountCacheItem.CreateAttempts++; if (createAccountCacheItem.CreateAttempts > 10) { ModelState.AddModelError(String.Empty, Languages.LanguageStrings.TooManyAttempts); } AddressOptions addressOptions = _accountProvider.GetAddressOptions(AddressOption.Billing); if (!ValidatePasswordComplexity(model.Password)) { ModelState.AddModelError(String.Empty, Languages.LanguageStrings.PasswordComplexityFailed); } if (!model.Password.Equals(model.ConfirmPassword)) { ModelState.AddModelError(String.Empty, Languages.LanguageStrings.PasswordDoesNotMatch); } if (addressOptions.HasFlag(AddressOptions.AddressLine1Mandatory) && String.IsNullOrEmpty(model.AddressLine1)) { ModelState.AddModelError(nameof(model.AddressLine1), Languages.LanguageStrings.AddressLine1Required); } if (addressOptions.HasFlag(AddressOptions.AddressLine2Mandatory) && String.IsNullOrEmpty(model.AddressLine2)) { ModelState.AddModelError(nameof(model.AddressLine2), Languages.LanguageStrings.AddressLine2Required); } if (addressOptions.HasFlag(AddressOptions.AddressLine3Mandatory) && String.IsNullOrEmpty(model.AddressLine3)) { ModelState.AddModelError(nameof(model.AddressLine3), Languages.LanguageStrings.AddressLine3Required); } if (addressOptions.HasFlag(AddressOptions.CityMandatory) && String.IsNullOrEmpty(model.City)) { ModelState.AddModelError(nameof(model.City), Languages.LanguageStrings.CityRequired); } if (addressOptions.HasFlag(AddressOptions.CountyMandatory) && String.IsNullOrEmpty(model.County)) { ModelState.AddModelError(nameof(model.County), Languages.LanguageStrings.CountyRequired); } if (addressOptions.HasFlag(AddressOptions.PostCodeMandatory) && String.IsNullOrEmpty(model.Postcode)) { ModelState.AddModelError(nameof(model.Postcode), Languages.LanguageStrings.PostcodeRequired); } if (addressOptions.HasFlag(AddressOptions.BusinessNameMandatory) && String.IsNullOrEmpty(model.BusinessName)) { ModelState.AddModelError(nameof(model.BusinessName), Languages.LanguageStrings.BusinessNameRequired); } if (addressOptions.HasFlag(AddressOptions.TelephoneMandatory) && String.IsNullOrEmpty(model.Telephone)) { ModelState.AddModelError(nameof(model.Telephone), Languages.LanguageStrings.TelephoneRequired); } createAccountCacheItem.CaptchaText = GetRandomWord(6, CaptchaCharacters); model.CaptchaText = String.Empty; }