0
我需要解密CookieAuthentication中間件默認名爲「.AspNet.Cookies」的OWIN cookie。如何爲CookieAuthentication中間件解密ASP.NET Cookie
我需要解密CookieAuthentication中間件默認名爲「.AspNet.Cookies」的OWIN cookie。如何爲CookieAuthentication中間件解密ASP.NET Cookie
// Get Cookie
var request = HttpContext.Request;
var cookie = request.Cookies.Get(".AspNet.Cookies");
var ticket = cookie.Value;
// Format Cookie to be converted
ticket = ticket.Replace('-', '+').Replace('_', '/');
var padding = 3 - ((ticket.Length + 3) % 4);
if (padding != 0)
ticket = ticket + new string('=', padding);
var bytes = Convert.FromBase64String(ticket);
// Decrypt
bytes = System.Web.Security.MachineKey.Unprotect(bytes,
typeof(CookieAuthenticationMiddleware).FullName,
"Cookies", // See below
"v1");
通過字節參數後,取消保護參數,被稱爲目的,這需要預期的一個匹配,以便能夠正確解密。否則你會得到CryptographicException。
的「曲奇」參數值相匹配:
(new CookieAuthenticationOptions()).AuthenticationType
可以構建ClaimsIdentity在上面的鏈接解釋或轉儲字節的字符串解密後。
對不起,我沒有詳細說明問題,因爲我添加了我在自己的答案中嘗試的內容。 – AlejandroC
幾天來一直在研究這個話題,並且找到了答案連接在一起的許多來源。只是想分享調查結果。 :) – AlejandroC