Cookies in .NET Core 2

From Logic Wiki
Revision as of 11:33, 13 April 2018 by AliIybar (Talk | contribs) (Created page with "Category:ASP.NET Category:dotnet Category:Core == Write with timeout == <pre class="brush:csharp;"> CookieOptions option = new CookieOptions(); if (expire...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Write with timeout

   CookieOptions option = new CookieOptions();  
   if (expireTime.HasValue)  
         option.Expires = DateTime.Now.AddMinutes(expireTime.Value);  
   else  
         option.Expires = DateTime.Now.AddMilliseconds(10);  
   Response.Cookies.Append(key, value, option);  

Write without timeout

   Response.Cookies.Append(key, value);  

Read

//read cookie from IHttpContextAccessor  
string cookieValueFromContext = _httpContextAccessor.HttpContext.Request.Cookies["key"];  

//read cookie from Request object  
string cookieValueFromReq = Request.Cookies["Key"];  

Remove Cookie

  Response.Cookies.Delete(key);   

CookieOptions

It extends the cookie behavior in the browser.

Options

  • Domain - The domain you want to associate with cookie
  • Path - Cookie Path
  • Expires - The expiration date and time of the cookie
  • HttpOnly - Gets or sets a value that indicates whether a cookie is accessible by client-side script or not.
  • Secure - Transmit the cookie using Secure Sockets Layer (SSL) that is, over HTTPS only.