.Net Core操作Cookie
Util方法定义
/// <summary>
/// Get the cookie
/// </summary>
/// <param name="key">Key </param>
/// <returns>string value</returns>
public string Get(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 Set(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 Remove(string key)
{
Response.Cookies.Delete(key);
}
Action里调用
//set the key value in Cookie
Set("CurrentUser", "Bob", 1);
//Delete the cookie object
Remove("CurrentUser");
仅供学习参考,如有侵权联系我删除