public CustomUserManager(IUserStore <BaseUser> store, IOptions <IdentityOptions> optionsAccessor, IPasswordHasher <BaseUser> passwordHasher, IEnumerable <IUserValidator <BaseUser> > userValidators, IEnumerable <IPasswordValidator <BaseUser> > passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger <UserManager <BaseUser> > logger) : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger) { UserValidators.Clear(); UserValidators.Add(new CustomUserValidator()); }
public IActionResult UsePasswordRecovery([FromBody] PasswordRecoveryDto model) { var user = context.Users.FirstOrDefault(a => a.RecoveryCode.Equals(model.Code)); if (user == null) { return(BadRequest("Podane błędne dane")); } if (!UserValidators.CheckIfPasswordIsCorrect(model.Password)) { return(BadRequest("Hasło musi posiadać wielką i małą literę, liczbę oraz znak specjalny a także składać się co najmniej z 8 znaków")); } CreatePasswordHash(model.Password, out byte[] passwordHash, out byte[] passwordSalt); user.RecoveryCode = null; user.RecoveryExpirationDate = null; user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; try { context.SaveChanges(); Logger.Log($"{nameof(UsersController)} {nameof(UsePasswordRecovery)}", $"Hasło zostało zmienione dla użytkownika o id: {user.Id}", NLog.LogLevel.Info); } catch (Exception) { return(BadRequest()); } return(Ok()); }
public User(string firstName, string lastName, string email, Address addresss) { UserValidators.ValidFirstName(firstName); UserValidators.ValidLastName(lastName); UserValidators.ValidEmail(email); FirstName = firstName; LastName = lastName; LastName = lastName; Email = email; Addresss = addresss; }
public IActionResult Register([FromBody] UserRegisterDto userDto) { if (!UserValidators.CheckIfEmailIsCorrect(userDto.Email)) { return(BadRequest("Podaj poprawny email")); } if (string.IsNullOrEmpty(userDto.Login)) { return(BadRequest("Podaj poprawny login")); } if (string.IsNullOrWhiteSpace(userDto.Password)) { return(BadRequest("Podaj poprawne hasło")); } if (context.Users.Any(x => x.Email.Equals(userDto.Email))) { return(BadRequest($"Adres email {userDto.Email} jest już zajęty")); } if (context.Users.Any(x => x.Login.Equals(userDto.Login))) { return(BadRequest($"Login {userDto.Login} jest już zajęty")); } CreatePasswordHash(userDto.Password, out byte[] passwordHash, out byte[] passwordSalt); var user = new User { Login = userDto.Login, Email = userDto.Email, PasswordHash = passwordHash, PasswordSalt = passwordSalt }; context.Users.Add(user); try { context.SaveChanges(); return(Ok()); } catch (Exception ex) { Logger.Log($"{nameof(UsersController)} {nameof(Register)}", ex.Message, NLog.LogLevel.Warn, ex); return(BadRequest(ex.Message)); } }
public User(string name, string surname, string email, Password password) { UserValidators.ValidIfNameExist(name); UserValidators.ValidIfSurnameExist(surname); UserValidators.ValidIfEmailExist(email); UserValidators.ValidIfPasswordExist(password); EmailValidators.ValidEmail(email); Name = name; Surname = surname; Email = email; PasswordHash = password.PasswordHash; PasswordSalt = password.PasswordSalt; Token = new Token(this); }
public IActionResult ChangePassword([FromBody] UserPasswordResetDto model) { var userId = ClaimsReader.GetUserId(Request.HttpContext.Request); var user = context.Users.SingleOrDefault(a => a.Id == userId); if (user == null) { return(NotFound()); } if (!UserValidators.CheckIfPasswordIsCorrect(model.Password)) { return(BadRequest("Hasło musi posiadać wielką i małą literę, liczbę oraz znak specjalny a także składać się co najmniej z 8 znaków")); } try { if (!VerifyPasswordHash(model.OldPassword, user.PasswordHash, user.PasswordSalt)) { return(BadRequest()); } } catch (Exception) { return(BadRequest()); } CreatePasswordHash(model.Password, out byte[] passwordHash, out byte[] passwordSalt); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; try { context.SaveChanges(); } catch (Exception) { return(BadRequest()); } return(Ok()); }
public UserManager(IUserStore <TUser> store, IOptions <AuthOptions> options, IPasswordHasher <TUser> passwordHasher, IEnumerable <IUserValidator <TUser> > userValidators, IEnumerable <IPasswordValidator <TUser> > passwordValidators, AuthErrorDescriber errors, IServiceProvider services, ILogger <UserManager <TUser> > logger) { if (store == null) { throw new ArgumentNullException(nameof(store)); } Store = store; Options = options?.Value ?? new AuthOptions(); PasswordHasher = passwordHasher; ErrorDescriber = errors; Logger = logger; if (userValidators != null) { foreach (var v in userValidators) { UserValidators.Add(v); } } if (passwordValidators != null) { foreach (var v in passwordValidators) { PasswordValidators.Add(v); } } _services = services; if (Options.Stores.ProtectPersonalData) { if (!(Store is IProtectedUserStore <TUser>)) { throw new InvalidOperationException("Store is not a protected user store!"); } if (services.GetService <ILookupProtector>() == null) { throw new InvalidOperationException("There's no personal data protector!"); } } }
/// <summary> /// Creates the specified <paramref name="user" /> in the backing store with no password, as /// an asynchronous operation. /// </summary> /// <param name="user"> The user to create. </param> /// <returns> /// The <see cref="T:System.Threading.Tasks.Task" /> that represents the asynchronous /// operation, containing the <see cref="T:Microsoft.AspNetCore.Identity.IdentityResult" /> /// of the operation. /// </returns> public override async Task <IdentityResult> CreateAsync(IdentityUserEntity user) { ThrowIfDisposed(); if (SupportsUserSecurityStamp) { if (Store is IUserSecurityStampStore <IdentityUserEntity> store) { await store.SetSecurityStampAsync(user, Guid.NewGuid().ToString(), CancellationToken); } else { throw new NotImplementedException(); } } user.NormalizedName = user.Name.ToUpper(); user.NormalizedEmail = user.Email.ToUpper(); user.LockoutEnabled = SupportsUserLockout; var validator = UserValidators.SingleOrDefault(v => v.GetType() == typeof(UserValidator <IdentityUserEntity>)); IdentityResult result; if (validator != null) { result = await validator.ValidateAsync(this, user); } else { result = await UserValidators.Last().ValidateAsync(this, user); } if (!result.Succeeded) { return(result); } var res = await Store.CreateAsync(user, CancellationToken); return(IdentityResult.Success); }