2016-11-18 119 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