Get the App
SLTechnology News&Howtos  ›  Development  › 

How to determine whether the browser accepts Cookie in ASP.NET

Shulou Source: shulou.com Published: 2022-06-02 07:58:50 09月29日 Update

This article introduces the knowledge about "how to determine whether the browser accepts cookies in ASP.NET". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to handle these situations! I hope you can read carefully and learn something!

One way to determine whether a browser accepts cookies in ASP.NET is to write a Cookie and then try to read it. If this Cookie cannot be read, it can be assumed that the browser does not accept cookies.

I've written a simple example of how to test whether cookies are accepted. This example contains two pages. Of the *** pages, I wrote a Cookie and redirected the browser to the second page. The second page attempts to read this Cookie, which in turn redirects the browser to *** pages and adds a query string variable with the test result to the URL.

The code for the *** pages in ASP.NET that check whether your browser accepts cookies is as follows:

protected System.Web.UI.WebControls.Label labelAcceptsCookies; private void Page_Load(object sender, System.EventArgs e) ... { if(! IsPostBack) ... { WriteCookie(); } } private void WriteCookie() ... { if( Request.QueryString["AcceptsCookies"] ==null) ... { Response.Cookies["TestCookie"].Value = "ok"; Response.Cookies["TestCookie"].Expires = DateTime.Now.AddMinutes(1); Response.Redirect("CookieRead.aspx? redirect="+Server.UrlEncode(Request.Url.ToString())); } else ... { labelAcceptsCookies.Text = "Accepts Cookies = "+Request.QueryString["AcceptsCookies"]; } }

*** Pages to test whether there is a reply, if not, search query string variables containing test results (AcceptsCookies). If the query string variable is not found, the test is not complete, and the code writes out a Cookie named "TestCookie." After writing the Cookie, the example calls Response.Redirect to switch to the test page (TestForCookies.aspx). Attached to the URL of the test page is a query string variable called redirect, which contains the URL of the current page so that it redirects to that page after the test is executed.

Test pages can consist entirely of code and do not need to contain controls. Here is the code used to test whether your browser accepts cookies:

private void Page_Load(object sender, System.EventArgs e) ... { ReadCookie(); } private void ReadCookie() ... { String redirect = Request.QueryString["redirect"]; String acceptsCookies ; //Do you accept cookies? if(Request.Cookies["TestCookie"] ==null) //No cookies, so no need to accept acceptsCookies = "0"; else ... { acceptsCookies = "1"; //Delete test cookies Response.Cookies["TestCookie"].Expires = DateTime.Now.AddDays(-1); } Response.Redirect(redirect + "? AcceptsCookies=" +acceptsCookies, true); }" How to determine whether the browser accepts cookies in ASP.NET "is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Tags: Pages tests browsers variables codes characters strings queries examples content more knowledge results try practical successful learning and then two reply Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Technology Redmi Apple Huawei