RSS

Cookie in ASP.NET Core

07 Dec
  1. using Microsoft.AspNetCore.Http;
  2. /// <summary>  
  3.     /// Get the cookie  
  4.     /// </summary>  
  5.     /// <param name=”key”>Key </param>  
  6.     /// <returns>string value</returns>  
  7.     public string GetCookie(string key)  
  8.     {  
  9.         return Request.Cookies[key];  
  10.     }  
  11.     /// <summary>  
  12.     /// set the cookie  
  13.     /// </summary>  
  14.     /// <param name=”key”>key (unique indentifier)</param>  
  15.     /// <param name=”value”>value to store in cookie object</param>  
  16.     /// <param name=”expireTime”>expiration time</param>  
  17.     public void SetCookie(string key, string value, int? expireTime)  
  18.     {  
  19.         CookieOptions option = new CookieOptions();  
  20.         if (expireTime.HasValue)  
  21.             option.Expires = DateTime.Now.AddMinutes(expireTime.Value);  
  22.         else  
  23.             option.Expires = DateTime.Now.AddMilliseconds(10);  
  24.             Response.Cookies.Append(key, value, option);  
  25.     }  
  26.     /// <summary>  
  27.     /// Delete the key  
  28.     /// </summary>  
  29.     /// <param name=”key”>Key</param>  
  30.     public void RemoveCookie(string key)  
  31.     {  
  32.         Response.Cookies.Delete(key);  
  33.     } 
 
Leave a comment

Posted by on December 7, 2021 in asp.net, Core

 

Leave a comment