public IEnumerable <ApplicationEnvironmentTokenViewModel> GetForEnvironment(long applicationId, long environmentId) { var tokens = (from at in _db.ApplicationTokens.Active().Build() join aet in _db.ApplicationEnvironmentTokens.Active() .Where(ae => ae.ApplicationEnvironment.EnvironmentId == environmentId) on new { at.ApplicationTokenId, environmentId } equals new { aet.ApplicationTokenId, environmentId = aet.ApplicationEnvironment.EnvironmentId } into aetj from aet in aetj.DefaultIfEmpty() where at.ApplicationId == applicationId select new ApplicationEnvironmentTokenViewModel() { ApplicationId = applicationId, EnvironmentId = environmentId, Name = at.Name, Value = (null == aet) ? null : aet.Value, ApplicationEnvironmentTokenId = (null == aet) ? -1 : aet.ApplicationEnvironmentTokenId }).ToList(); //Decrypt all of the tokens foreach (var token in tokens) { if (_encrypter.IsEncrypted(token.Value)) { token.Value = _encrypter.Decrypt(token.Value, GetEncryptionKey(token.ApplicationId)); } } return(tokens); }
public void EncryptApplications() { var applications = _db.Applications.Where(a => a.EncryptionKey == null); foreach (var application in applications) { application.EncryptionKey = _encrypter.GenerateKey(); var tokens = _db.ApplicationEnvironmentTokens.Where( aet => aet.ApplicationToken.ApplicationId == application.ApplicationId); foreach (var token in tokens) { if (!_encrypter.IsEncrypted(token.Value)) { token.Value = _encrypter.Encrypt(token.Value, application.EncryptionKey); } } } _db.SaveChanges(); }