如何读取点网核心中的cookie到期?

me_*_*ine 7 cookies asp.net-core-mvc .net-core asp.net-core asp.net-core-1.0

我看到您可以在将新Cookie附加到响应时使用CookieOptions设置到期时间。但是,HttpContext.Request.Cookies返回一个IRequestCookieCollection,它似乎只为您提供键/值对。

是否可以读取请求Cookie的CookieOptions(特别是到期)?

我正在使用.Net Core(1.0.0-preview2-003131)

注意:我需要在请求处理中读取任意cookie的到期时间,而不是框架生成的auth cookie的到期时间。

Dab*_*oul 5

我是通过对事件“OnValidatePrincipal”做出反应来得到的

app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
... 
Events = new CookieAuthenticationEvents
{
OnValidatePrincipal = CookieAuthenticationEventHandler.ValidatePrincipalAsync
}
});
Run Code Online (Sandbox Code Playgroud)

然后我可以访问,一旦主体被验证,cookie 将在我的 ValidatePrincipalAsync 中过期时使用属性 context.Properties.ExpiresUtc

为了能够稍后在我的控制器中获得 expires 属性,我以这种方式将它添加到我的 HttpContext 中:

context.Request.HttpContext.Items.Add("ExpiresUTC", context.Properties.ExpiresUtc);
Run Code Online (Sandbox Code Playgroud)

但我对那部分不太满意,也许有人可以给你一个更好的方法来直接在控制器中访问它,而不必从 OnValidatePrincipal 中获取它。