Overview
I am not aware of any JavaScript methods that mimic C#'s options functionality.
Two JavaScript functions I have previously written are setCookie
and getCookieValue
for setting cookies.
var common = common || {}
common.cookieManager = common.cookieManager || {};
common.cookieManager.getCookieValue = (name) => (
document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)')?.pop() || ''
)
/**
* set cookie with name value and life time
* @param {string} name cookie name
* @param {string} value cookie value
* @param {number} expireTime s
*/
common.cookieManager.setCookie = async (name, value, expireTime = 0) => {
const expires = (new Date(Date.now() + expireTime * 1000)).toUTCString();
document.cookie = `${name}=${value}; expires=` + expires + ";path=/;"
}
You can customize the setCookie function by adding parameters based on Document.cookie
However, if you are using the .NET MVC framework, it may be more advantageous to set cookies in the backend.
In your .NET WebApi Controller, you should utilize the Response
property.
Utilize Controller.Response
to set cookies using the CookieOptions class
Example code:
Response.Cookies.Append("key", "value", new CookieOptions() { Expires = DateTime.Now.AddMinutes(30), IsEssential = true });