//Method for password validation public string PasswordValidation(string password) { Core.Error.Error error = new Core.Error.Error(); error.Name = "Password error"; if (password == "") error.Definition += _listOfErrors[1015].Definition + "<br/>"; Regex reg; if (password.Length < 6) error.Definition += _listOfErrors[1011].Definition + "<br/>"; reg = new Regex(@"[a-z]{1,}"); if (!reg.Match(password).Success) error.Definition += _listOfErrors[1013].Definition + "<br/>"; reg = new Regex(@"[A-Z]{1,}"); if (!reg.Match(password).Success) error.Definition += _listOfErrors[1014].Definition + "<br/>"; reg = new Regex(@"[\W]{1,}"); if (!reg.Match(password).Success) error.Definition += _listOfErrors[1012].Definition + "<br/>"; return error.ToJson(); }
//Method for user registration public async Task<string> Register(string firstName, string lastName, string email, string password, string captchaResponse) { try { //CAPTCHA validation bool IsCaptchaValid = reCaptchaClass.Validate(captchaResponse) == "True" ? true : false; if (IsCaptchaValid) { //Checking user data if (firstName == null || lastName == null) return _listOfErrors[1007].ToJson(); if (email == null) return _listOfErrors[1008].ToJson(); //Сhecking e-mail on the registered if (UserProfileRepository.UserIsRegistered(email)) { return _listOfErrors[1009].ToJson(); } //Creating new user var user = new ApplicationUser { UserName = email, Email = email, FirstName = firstName, LastName = lastName, EmailConfirmed = false }; var result = await _userManager.CreateAsync(user, password); //Sending confirmation email of registration if (result.Succeeded) { //Add user role await _userManager.AddToRoleAsync(user, "User"); //Generation message body var callbackUrl = GetLinkForUser(user, "emailconfirmed", "Account", "ConfirmEmail"); //Add message tempalate string template = ResourceReader.GetTemplate("..//Templates//Message.cshtml").Replace("@User", firstName); //Sending message to user e-mail SendEmail(email, "Confirm your account", template + "<a href=\"" + callbackUrl + "\">Registration confirmation</a>"); //User LogOff await _signInManager.SignOutAsync(); return _listOfErrors[2002].ToJson(); } else { string errors = ""; //Get list of errors foreach (var i in result.Errors) errors += new Core.Error.Error(int.Parse(i.Code), "Error", i.Description).ToJson(); return errors; } } else return _listOfErrors[1001].ToJson(); } catch (Exception ex) { return new Core.Error.Error(ex.HResult, ex.Source, ex.Message).ToJson(); } }
//Method for e-mail validation public string EmailValidation(string email) { Core.Error.Error error = new Core.Error.Error(); if (email==null) error.Definition += _listOfErrors[1017].ToJson(); Regex reg = new Regex(@"^([a-z0-9_-]+\.)*[a-z0-9_-]+@[a-z0-9_-]+(\.[a-z0-9_-]+)*\.[a-z]{2,6}$"); if (!reg.Match(email).Success) error.Definition += _listOfErrors[1016].ToJson(); return error.ToJson(); }
//Method for reset password public async Task<string> ResetPassword(string email, string password, string code) { try { //Checking e-mail identity code if (code == null) return _listOfErrors[1006].ToJson(); if (password == null) return _listOfErrors[1004].ToJson(); //Check e-mail on the registered if (UserProfileRepository.UserIsRegistered(email)) { var user = await _userManager.FindByNameAsync(email); //Reset user password var result = await _userManager.ResetPasswordAsync(user, code, password); if (result.Succeeded) { //User logIn await _signInManager.PasswordSignInAsync(email, password, true, lockoutOnFailure: false); return _listOfErrors[2007].ToJson(); } else { string errors = ""; //Get list of errors foreach (var i in result.Errors) errors += new Core.Error.Error(int.Parse(i.Code), "Error", i.Description).ToJson(); return errors; } } return _listOfErrors[1005].ToJson(); } catch (Exception ex) { return new Core.Error.Error(ex.HResult, ex.Source, ex.Message).ToJson(); } }