private static bool TryDecryptUserId(IKeyFactory keyFactory, out Guid userId, string UserSession, ILogger <UserIdMiddleWare> logger) { try { using Aes aes = Aes.Create(); aes.Key = keyFactory.Create(_keyname); aes.IV = keyFactory.Create(_ivname); ICryptoTransform decryptor = aes.CreateDecryptor(); using MemoryStream ms = new MemoryStream(Convert.FromBase64String(UserSession)); using CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read); using StreamReader sr = new StreamReader(cs); string decryptedUserSession = sr.ReadToEnd(); return(Guid.TryParse(decryptedUserSession, out userId)); } catch (Exception e) { logger.LogWarning(e, "Unable to decrypt the user session cookie."); userId = default; return(false); } }
private static string EncryptUserId(IKeyFactory keyFactory, Guid userId) { using Aes aes = Aes.Create(); aes.Key = keyFactory.Create(_keyname); aes.IV = keyFactory.Create(_ivname); ICryptoTransform encryptor = aes.CreateEncryptor(); using MemoryStream ms = new MemoryStream(); using CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write); using StreamWriter sw = new StreamWriter(cs); sw.Write(userId.ToString()); sw.Flush(); cs.FlushFinalBlock(); return(Convert.ToBase64String(ms.ToArray())); }
public async Task <Key> FindOrCreateAsync(Guid uuid, KeyType type, CancellationToken cancellationToken) { var keyMaybe = await FindAsync(k => k.Uuid == uuid && k.KeyType == type, cancellationToken); if (keyMaybe.HasValue) { return(keyMaybe.Value); } var key = _keyFactory.Create(uuid, type); await AddAsync(key, cancellationToken); return(key); }
public void DoWork() { var instance = _factory.Create(ProcessKey.A); instance.Run(); }