Determine whether the browser accepts cookies in ASP. NET.

Source: Internet
Author: User

In asp.net, one way to determine whether the browser accepts a Cookie is to write a Cookie before trying to read it. If the Cookie cannot be read, the browser may not accept the Cookie.

I wrote a simple example to illustrate how to test whether a Cookie is accepted. This example contains two pages. On the first page, I wrote a Cookie and redirected the browser to the second page. On the second page, try to read the Cookie, redirect the browser to the first page, and add a query string variable with test results to the URL.

The code for checking whether the browser accepts cookies on the first page of asp.net is as follows:

 
 
  1. ProtectedSystem. Web. UI. WebControls. Label labelAcceptsCookies;
  2. Private VoidPage_Load (ObjectSender, System. EventArgs e)
  3. ...{
  4. If(! IsPostBack)
  5. ...{
  6. WriteCookie ();
  7. }
  8. }
  9.  
  10. Private VoidWriteCookie ()
  11. ...{
  12. If(Request. QueryString ["AcceptsCookies"] =Null)
  13. ...{
  14. Response. Cookies ["TestCookie"]. Value ="OK";
  15. Response. Cookies ["TestCookie"]. Expires = DateTime. Now. AddMinutes (1 );
  16. Response. Redirect ("CookieRead. aspx? Redirect ="+ Server. UrlEncode (Request. Url. ToString ()));
  17. }
  18. Else 
  19. ...{
  20. LabelAcceptsCookies. Text ="Accept Cookie ="+ Request. QueryString ["AcceptsCookies"];
  21. }
  22. }

On the first page, test whether there is a response. If not, search for the query string variable (AcceptsCookies) that contains the test result ). If the query string variable is not found, the test is not complete, and the code writes a Cookie named "TestCookie. After the Cookie is written, the following example calls Response. Redirect to switch to the test page (TestForCookies. aspx ). The URL appended to the test page is the query string variable named redirect, which contains the URL of the current page, so that you can redirect to the page after the test is executed.

The test page can be fully composed of code and does not need to contain controls. The following code is used to test whether the browser accepts cookies:

 
 
  1. Private VoidPage_Load (ObjectSender, System. EventArgs e)
  2. ...{
  3. ReadCookie ();
  4. }
  5. Private VoidReadCookie ()
  6. ...{
  7. String redirect = Request. QueryString ["Redirect"];
  8. String acceptsCookies;
  9. // Do you want to accept cookies? 
  10. If(Request. Cookies ["TestCookie"] =Null)
  11. // No Cookie, so you do not need to accept 
  12. AcceptsCookies ="0";
  13. Else 
  14. ...{
  15. AcceptsCookies ="1";
  16. // Delete the test Cookie 
  17. Response. Cookies ["TestCookie"]. Expires = DateTime. Now. AddDays (-1 );
  18. }
  19. Response. Redirect (redirect +"? AcceptsCookies ="+ AcceptsCookies,True);
  20. }

In this way, you can determine whether the browser accepts cookies in ASP. NET.

  1. Implement code that prevents multiple page submissions in ASP. NET
  2. ASP. NET Server Control Development-Composite Control
  3. Introduction to "three-layer structure" in ASP. NET
  4. 26 methods for optimizing ASP. NET performance
  5. Comparison of html controls and web controls in ASP. NET

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.