ASP.NET and ASP interconnect COOKIES experience

  • 2020-05-09 18:25:09
  • OfStack

After Microsoft launched. NET and carried out large-scale promotion and popularization, 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, it is faced with a problem: when ASP and ASP.NET are integrated with each other, the Chinese COOKIES information cannot be Shared with each other. When ASP.NET is used to write the Chinese COOKIES information, ASP is used to read it.

Later, the source of the problem was found through searching for materials and continuous practice. The reason why the Chinese COOKIES information could not be read correctly in ASP was that its Chinese encoding format was different.

The development project Web.config configuration file contains the following information:

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

Accordingly, 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:

Write COOKIE as follows:
 
  '---------------------------------------------------------------- 
  ' Name:  WriteCookie(ByVal strCookieName As String, ByVal strKeyName As String, ByVal strKeyValue As String) 
  ' Parameters: 
  '  [ByVal] strCookieName - cookie The name  
  '  [ByVal] strKeyName -  The key name  
  '  [ByVal] strKeyValue -  The key value.  
  ' 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 implements ASP can read correctly ASP. NET COOKIES information written Chinese, but at the same time if the ASP NET will read over Chinese COOKIES information directly at the front desk display also shows to the code, this is because the ASP. NET displayed in "utf - 8" encoding "GB2312" encoding information in Chinese, so there is a problem, can use the following method at the front desk page correctly display Chinese information:

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 has solved the problem that Chinese COOKIES information cannot be Shared in ASP and ASP.NET. However, I wonder if it is possible to modify the encoding method in Web.config configuration file and change the encoding method of "utf-8" to "GB2312" to solve the above problem.

In ASP. NET, we clean COOKIES information as follows:
 
  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 COOKIES of ASP.NET when the COOKIES information is cleared by the above method and the COOKIES information is logged in again. The reason is unknown, but adding the following statement to the COOKIES information method above can solve the problem that COOKIES information cannot be properly cleared:

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

The above is for myself in the work 1 small experience summary, may have the inaccurate place, hoped everybody to criticize is correct!

Related articles: