- using Microsoft.AspNetCore.Http;
- /// <summary>
- /// Get the cookie
- /// </summary>
- /// <param name=”key”>Key </param>
- /// <returns>string value</returns>
- public string GetCookie(string key)
- {
- return Request.Cookies[key];
- }
- /// <summary>
- /// set the cookie
- /// </summary>
- /// <param name=”key”>key (unique indentifier)</param>
- /// <param name=”value”>value to store in cookie object</param>
- /// <param name=”expireTime”>expiration time</param>
- public void SetCookie(string key, string value, int? expireTime)
- {
- 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);
- }
- /// <summary>
- /// Delete the key
- /// </summary>
- /// <param name=”key”>Key</param>
- public void RemoveCookie(string key)
- {
- Response.Cookies.Delete(key);
- }
Cookie in ASP.NET Core
07
Dec