public async void LoginHandler() { try { _user.UserName = _user.UserName.Trim(); string hashedPassword = new SHA512Crypto().GetHashBase64String(_user.Password); LoggedUserModel loggedUser = new LoggedUserModel(); await Task.Run(() => { using (CoreContext context = new CoreContext()) { User user = context.Users.FirstOrDefault(x => x.UserName.Equals(_user.UserName, StringComparison.InvariantCultureIgnoreCase) && x.PasswordHash.Equals(hashedPassword)); if (user != null) { loggedUser.ID = user.ID; loggedUser.UserName = user.UserName; loggedUser.FullName = user.FullName; loggedUser.Email = user.Email; loggedUser.IsAuthenticated = true; } else { loggedUser.IsAuthenticated = true; } } }); GoToMainViewRequested?.Invoke(this, loggedUser); } catch (Exception ex) { LogHelper.LogException(new string[] { ex.ToString() }); } }
/// <summary> /// Creates secure password based on the predefined input. /// Size of bytes determines the size of returned password. /// Iteration determines how many iterations to take to produce final result. /// </summary> /// <param name="input">Input as string. It's called predefined input.</param> /// <param name="size">Size of returned password in bytes.</param> /// <param name="iteration">Iteration count.</param> /// <returns>Random password in bytes.</returns> /// <exception cref="ArgumentNullException">'input' cannot be null/empty.</exception> /// <exception cref="ArgumentNullException">'size' cannot be less than 8 bytes.</exception> /// <exception cref="ArgumentOutOfRangeException">'iteration' is invalid.</exception> public static byte[] CreateSecurePassword(string input, int size = 16, int iteration = 10000) { if (String.IsNullOrEmpty(input)) { throw new ArgumentNullException(nameof(input)); } if (size < 8) { throw new ArgumentNullException(nameof(size), "'size' cannot be less than 8."); } if (iteration < 5000) { throw new ArgumentOutOfRangeException(nameof(iteration), "Min value for iteration is 5000."); } byte[] data = null; try { byte[] salt = new SHA512Crypto().GetHashBytes(input); using (Rfc2898DeriveBytes generator = new Rfc2898DeriveBytes(input, salt, iteration)) { data = generator.GetBytes(size); } } catch { data = null; } return(data); }