ASP and ASP.NET share a little experience with COOKIES

  • 2020-05-05 11:08:54
  • OfStack

  was launched by Microsoft. NET and popularized on a large scale, ASP. NET gradually entered the mainstream of information system development. However, at the same time, the old system surface developed with ASP is about to be integrated. At this time, there is a problem: when ASP and ASP.NET are integrated with each other, their Chinese COOKIES information cannot be Shared.
      finally found the root of the problem by looking up materials and practicing constantly. The information of COOKIES in Chinese cannot be read correctly in ASP because of its different Chinese encoding format.
The       development project Web.config configuration file has the following information:

      < ! --   globalization                 this section sets the globalization Settings for the application.       - >
      < globalization requestEncoding="utf-8" responseEncoding="utf-8" / >

      seems to be, in the absence of carrying out the special configuration, the Chinese COOKIES information in the system default is passed through the "utf - 8" encoding, and Chinese COOKIES ASP information default is passed through the "GB2312" encoding, so ASP with ASP. NET COOKIES information by default in Chinese cannot be Shared each other, but can be solved by the following methods:
      writes COOKIE in the following way:

              '----------------------------------------------------------------
              ' Name:               WriteCookie(ByVal strCookieName As String, ByVal strKeyName As String, ByVal strKeyValue As String)
              ' Parameters:
            '      [ByVal] strCookieName - cookie name
              '      [ByVal] strKeyName - key name
            '      [ByVal] strKeyValue - key.
              ' Return:           String
              '----------------------------------------------------------------
              Public Function WriteCookie(ByVal strCookieName As String, ByVal strKeyName As String, ByVal strKeyValue As String) As String
                      Dim objEnc As System.Text.Encoding = System.Text.Encoding.GetEncoding("GB2312")
                      strKeyValue = System.Web.HttpUtility.UrlEncode(strKeyValue, objEnc)
                      System.Web.HttpContext.Current.Response.Cookies(strCookieName)(strKeyName) = strKeyValue
              End Function

      above method realizes that ASP can correctly read the Chinese COOKIES information written by ASP.NET, but at the same time, if the Chinese COOKIES information read by ASP. You can use the following method to correctly display Chinese information on the foreground page:

              Dim uName As String = System.Web.HttpContext.Current.Server.UrlDecode(System.Web.HttpContext.Current.Request.Cookies(strCookieName)(strKeyName))
              Dim objEnc As System.Text.Encoding = System.Text.Encoding.GetEncoding("GB2312")
              uName = System.Web.HttpUtility.UrlDecode(uName, objEnc)

The above method of       solves the problem that the information of Chinese COOKIES cannot be Shared in ASP and ASP.NET. However, I wonder if it is possible to solve the above problem by modifying the encoding mode in the configuration file of Web.config and changing the encoding mode of "utf-8" to "GB2312".

      in ASP.NET we generally clear COOKIES information by:

              Sub CleanCookies()
                      Dim i As Integer
                      Dim cookie As System.Web.HttpCookie
                      For i = 0 To System.Web.HttpContext.Current.Request.Cookies.Count - 1
                              cookie = System.Web.HttpContext.Current.Request.Cookies(i)
                              System.Web.HttpContext.Current.Response.Cookies(CStr(cookie.Name)).Value = ""
                              System.Web.HttpContext.Current.Response.Cookies(CStr(cookie.Name)).Expires = DateAdd(DateInterval.Day, -1, Now)
                      Next
              End Sub

      however, ASP will not be able to read ASP.NET COOKIES when the COOKIES information is cleared by the above method and logged in again

              System.Web.HttpContext.Current.Response.Cookies.Clear()

Above       is a summary of my experience in work. There may be some inaccuracies. I hope you can correct me.

 


Related articles: