public static async Task <byte[]> EncryptPassword(string password, string userId, DatabaseContext _context) { using var deriveBytes = new Rfc2898DeriveBytes(password, PBKDF2[0], PBKDF2[1]); var salt = new SaltModel { UserModelId = userId, Value = deriveBytes.Salt }; var key = deriveBytes.GetBytes(PBKDF2[0]); var userSalt = await _context.Salts.Where(s => s.UserModelId == userId).FirstOrDefaultAsync(); if (userSalt != null) { userSalt.Value = salt.Value; _context.Entry(userSalt).State = EntityState.Modified; } else { await _context.AddAsync(salt); } return(key); }
//Update existing salt public async void Update(ObjectId id, SaltModel salt) => await Task.Run(() => _salt.ReplaceOne(x => x.Id == id, salt));
//Add new user salt public string Create(SaltModel salt) { Task.Run(() => _salt.InsertOne(salt)); return(""); }