2016-11-18 138 views
1

在Asp.Net內核中,當您將應用配置爲app.UseSession()時,會創建一個cookie。 默認情況下,Cookie被稱爲「.AspNetCore.Session」。它的值標識要使用的會話。目前,我正在將會話數據保存在sql服務器上。我需要知道「.AspNetCore.Session」的解密值,以便我可以查找數據庫中的會話。在ASP.NET Core中解密「.AspNetCore.Session」cookie

有沒有辦法解密這個值?我知道ASP.NET必須以某種方式在幕後進行。

+0

不知是否是相同的加密/解密功能形式的擔保。也許無論FormsAuthentication.Decrypt()正在使用可能工作或至少值得一試。 –

+1

它是一個'IDataProtector',接着是base64-編碼。爲了解密,你首先需要base64解碼成一個字節數組,然後應用下面的一部分:http://stackoverflow.com/a/37543433/1132334。 「祕密」是這個部分:'CreateProtector(「Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware」,...)' – dlatikay

+0

感謝您的指導@dlatikay。從您提供的鏈接中,我可以在C:\ mypath中輸入什麼內容? –

回答

2

session source擁有一切,但您應該需要知道它,ISessionStoreIDistributedSessionStore爲您提供了一個會話密鑰以供使用。

不是對cookie格式做出假設,而是阻止您使用商店API?

+0

ISessionStore只有一個定義了Create方法的公共成員。那麼如何使用它來獲取SessionKey,這是存儲會話數據的sql server表的關鍵? –

-1
private readonly IDataProtector _dataProtector; 


public DiscussionController(IDataProtectionProvider dataProtectionProvider) 
{ 
    _dataProtector = dataProtectionProvider.CreateProtector(nameof(SessionMiddleware)); 
} 

public ViewResult Index() 
{  

    string cookieValue; 
    HttpContext.Request.Cookies.TryGetValue(".AspNetCore.Session", out cookieValue); 

    var protectedData = Convert.FromBase64String(Pad(cookieValue)); 

    var unprotectedData = _dataProtector.Unprotect(protectedData); 

    string humanReadableData = System.Text.Encoding.UTF8.GetString(unprotectedData); 
} 

墊功能是從https://github.com/aspnet/Session/blob/dev/src/Microsoft.AspNetCore.Session/CookieProtection.cs