public async Task SetPinAsync(RequestorInfo requestor, uint?pinType, string pin) { var sha = SHA256.Create(); var loaded = await _dbContext.Pins.FirstOrDefaultAsync(x => x.HouseholdId == requestor.HouseholdId && x.ProfileId == requestor.ProfileId && x.PinType == pinType); loaded.PinHash = sha.ComputeHash(Encoding.ASCII.GetBytes(loaded.PinSalt + pin)).ToHexString(); await _dbContext.SaveChangesAsync(); var prefix = _cacheKeyGenerator.GenerateKeyForHash(requestor, pinType); await _redisClient.Db0.AddAsync(prefix, loaded.PinHash); }
public async Task CreateOrUpdatePinAsync(RequestorModel requestor, PinModel pin) { var isNew = false; if (!requestor.PinType.HasValue) { requestor.PinType = 0; } var found = await _dbContext.Pins.SingleOrDefaultAsync(x => x.HouseholdId == requestor.HouseholdId && x.ProfileId == x.ProfileId && x.PinType == requestor.PinType); if (found is null) { found = new Pins() { OpcoId = requestor.OpCoId, HouseholdId = requestor.HouseholdId, ProfileId = requestor.ProfileId, PinType = requestor.PinType.Value }; isNew = true; } found.PinHash = pin.PinHash; found.PinLocked = pin.PinLocked; found.PinSalt = pin.PinSalt; found.LockReason = pin.LockReason; if (isNew) { await _dbContext.Pins.AddAsync(found); } await _dbContext.SaveChangesAsync(); var key = _cacheKeyGenerator.GenerateKeyForPin(requestor.OpCoId, requestor.HouseholdId, requestor.ProfileId, requestor.PinType); _redisClient.Db0.Database.StringSet(key, SerializePinModel(pin)); }