For security reasons, some clients disable cookies. When you want to use cookies to store some information (especially some critical information), you need to determine whether the customer has disabled cookies. How to judge?
Since there is no direct judgment method, it can only be judged indirectly. The specific method is to set a cookie value, and then take this value to see if it can be obtained. It can be found that the cookie is not disabled; otherwise, the description is disabled. The specific implementation code is as follows:
1, set the cookie value method
/// <summary>
/// Set the cookie value
/// </summary>
/// <param name="CookieName"> Cookie Name </param>
/// <param name="val"> Pending Value </param>
/// <param name="expireTime"> expiration time </param>
public void SetCookie(string CookieName, string val, double expireTime)
{
Response.Cookies[cookieName].Value = val;
Response.Cookies[cookieName].Expires = DateTime.Now.AddHours(expireTime);
}
2, get the cookie value method
/// <summary>
/// Get the cookie value
/// </summary>
/// <param name="CookieName"> Cookie name </param>
/// <return> Expiration time </return>
public string GetCookie(string CookieName)
{
if (Request.Cookies[cookieName] != null)
return Request.Cookies[cookieName].Value.ToString();
else
return string.Empty;
}
3, judged in the page load event
protected void Page_Load(object sender, EventArgs e)
{
SetCookie("isDisable", "ok", 6);
if (GetCookie("isDisable") != string.Empty)
Response.Write("Cookie is not disabled!");
else
Response.Write("Cookie has been disabled!");
}
Post your comments / questions
Recent Article
- How to get domain name information from a Domain using Python
- ModulenotFoundError: no module named 'debug_toolbar' -SOLUTION
- How to create superuser in django project hosted in cPanel without terminal
- CSS & images not loading in django admin | cpanel without terminal
- Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects
- How to sell domain name on Godaddy (2023)
- TemplateSyntaxError at / Could not parse the remainder: ' + 1' from 'forloop.counter0 + 1'
- urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0
Related Article