using System.Collections.Specialized; using System.Security.Cryptography; using System.Text; public sealed class AesHmacCookieGenerator : IDisposable { private AesCryptoServiceProvider _aes; private readonly byte[] _hmacKey; public AesHmacCookieGenerator(byte[] encryptionKey, byte[] hmacKey) { _hmacKey = hmacKey; _aes = new AesCryptoServiceProvider { Mode = CipherMode.CBC, Key = encryptionKey }; IV = _aes.IV; } public byte[] IV { get; set; } public string GenerateCookieContent(NameValueCollection sessionData, byte[] iv) { var encodedSessionData = EncodeSessionData(sessionData); byte[] cipherText = EncryptSessionData(encodedSessionData, iv); byte[] mac = ComputeHMAC(cipherText, iv); return EncodeCookie(iv, mac, cipherText); } private static string EncodeSessionData(NameValueCollection sessionData) { var builder = new StringBuilder(); for (int i = 0; i < sessionData.Count; i++) { if (i > 0) builder.Append('&'); builder.Append(sessionData.Keys[i]); builder.Append('='); builder.Append(sessionData[i]); } return builder.ToString(); } private static string EncodeCookie(byte[] iv, byte[] mac, byte[] cipherText) { return Convert.ToBase64String(iv) + "$" + Convert.ToBase64String(mac) + "$" + Convert.ToBase64String(cipherText); } private byte[] EncryptSessionData(string encodedSessionData, byte[] iv) { byte[] plainTextBytes = Encoding.UTF8.GetBytes(encodedSessionData); using (MemoryStream memoryStream = new MemoryStream()) { using (ICryptoTransform encryptor = _aes.CreateEncryptor(_aes.Key, iv)) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); } return memoryStream.ToArray(); } } } private byte[] ComputeHMAC(byte[] ciphertext, byte[] iv) { byte[] messageToMac = new byte[iv.Length + ciphertext.Length]; Array.Copy(iv, 0, messageToMac, 0, iv.Length); Array.Copy(ciphertext, 0, messageToMac, iv.Length, ciphertext.Length); using (HMACSHA256 hmacsha256 = new HMACSHA256(_hmacKey)) { return hmacsha256.ComputeHash(messageToMac); } } public void Dispose() { if (_aes != null) { _aes.Dispose(); _aes = null; } } } /// /// IDP to write SOO cookies using sample data. /// public class Program { // Test Environment: // Limyee CMS application url: web.limyee.localhost // Current applicatin url: idp.limyee.localhost public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); app.MapGet("/", () => "Hello World!"); app.MapGet("/login", (HttpContext context) => { var encryptionKeyText = "exNWTNoCi/RLKfWmNdPlhSOOeS2dULvjakSKLLcnj6w="; var hmacKeyText = "This is my HMAC key!"; var sessionData = new NameValueCollection { { "username", "example" }, { "emailAddress", "example@example.org" }, { "roles", "Administrators" } }; var generator = new AesHmacCookieGenerator(Convert.FromBase64String(encryptionKeyText), Encoding.UTF8.GetBytes(hmacKeyText)); var cookieContent = generator.GenerateCookieContent(sessionData, generator.IV); string[] parts = cookieContent.Split('$'); if (parts.Length != 3) throw new Exception("SSO Cookie is invalid - expected 3 parts delimited by \'$\'"); var IV = parts[0]; var HmacOrTag = parts[1]; var CipherText = parts[2]; context.Response.Cookies.Append("AuthenticatedUser", cookieContent, new CookieOptions { Domain = ".limyee.localhost" }); context.Response.Redirect(context.Request.Query["ReturnUrl"].ToString()); }); app.MapGet("/logout", (HttpContext context) => { context.Response.Cookies.Delete(".Limyee", new CookieOptions { Domain = ".limyee.localhost" }); context.Response.Cookies.Delete("AuthenticatedUser", new CookieOptions { Domain = ".limyee.localhost" }); context.Response.Redirect(context.Request.Query["ReturnUrl"].ToString()); }); app.Run(); } }