C # HttpListener Cookies expiring after a session, even if an expiration time is set

I have a class HttpListenerfor handling logins on a page and can successfully set a cookie, but the cookie expires at the end of the session (using the cookie extension in Chrome, shows that cookie is a session cookie). Here is the code snippet:

CookieCollection ccol = new CookieCollection();    
Cookie loginCookie = new Cookie();

loginCookie.Name = "login";
loginCookie.Value = "loggedin";
loginCookie.Expires = DateTime.Now.AddMinutes(60);
ccol.Add(loginCookie);
context.Response.Cookies = ccol;

As long as I am in the same session, I can access the cookie without any problems.

+5
source share
2 answers

Typically, the cookie ( Cookies) collection property is read-only (at least when using the ASP.NET property Response.Cookies).

, HttpListener, , Cookies , cookie? , cookie, cookie?

:

-, HttpListener, , . cookie, http :

Set-Cookie: username=shiv

- :

Set-Cookie: username=shiv; expires=Thu, 27-Jan-2011 00:45:41 GMT; path=/

? ...

2:

, Expiration . ASP.NET , .

Http :

context.Response.Headers.Add("Set-Cookie", 
"username=shiv; expires=Thu, 27-Jan-2011 00:45:41 GMT; path=/");

context - HttpListenerContext.

, ( ) cookie HttpListener. DateTime , :

var cookieDate = DateTime.UtcNow.AddMinutes(60d).ToString("dddd, dd-MM-yyyy hh:mm:ss GMT");
+5

, :

string cookieDate = DateTime.UtcNow.AddMinutes(60).ToString("ddd, dd-MMM-yyyy H:mm:ss");

context.Response.Headers.Add("Set-Cookie", "cookieName=cookieValue;Path=/;Expires=" + cookieDate + " GMT");
+2

Source: https://habr.com/ru/post/1788062/


All Articles