Asp. NET and ASP Exchange cookies with a little experience _ practical skills

Source: Internet
Author: User
Tags httpcontext
Launched at Microsoft. NET and carried out a large-scale popularization after the ASP. NET gradually entered the mainstream of information system development. At the same time, however, the old system surface developed with ASP is being integrated, Faced with a problem: ASP and asp.net integration, which can not be shared cookies information, when the use of ASP.net to write Chinese cookies, using ASP for reading, read out is garbled, not Chinese.

Later, through the search for information, non-stop practice, finally found the root cause of the problem, Chinese cookies information in the ASP can not be correctly read the reason for its Chinese encoding format is different.

The following information is available in the development project Web.config configuration file:

<!--Globalization This section sets the globalization settings for your application. -->
<globalization requestencoding= "Utf-8" responseencoding= "Utf-8"/>

According to this, in the absence of special configuration of the project, the Chinese cookies in the system by default is passed through the "Utf-8" encoding method, while the ASP Chinese cookies information by default through the "GB2312" encoding way to pass, Therefore, the ASP and asp.net Chinese cookies information cannot be shared by default, but can be resolved by the following methods:

Write cookies in the following ways:
Copy Code code as follows:

'----------------------------------------------------------------
' 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 value.
' Return:string
'----------------------------------------------------------------
Public Function Writecookie (ByVal strcookiename As String, ByVal Strkeyname as String, ByVal strKeyValue as String) as Str Ing
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

The above method enables the ASP to correctly read asp.net written Chinese cookies information, but at the same time if the Chinese cookies in asp.net will be read in the foreground when the display is garbled, this is because the asp.net "utf-8" encoding mode display "GB2312" encoding the Chinese information, so there is a problem, you can use the following methods in the foreground page to display the correct 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 solves the problem that the Chinese cookie information cannot be shared in ASP and asp.net, but I do not know if we can modify the encoding mode of Web.config configuration file to change the "Utf-8" encoding mode to "GB2312" to solve the above problem. Should be possible, we can try, tell me the result.

In asp.net, we generally use the following methods to remove cookies information:
Copy Code code 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
cookies = 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, in the above method to remove cookies information and log on again when the ASP will not be able to read ASP.net cookies, the reason for unknown, but in the above method of clearing cookies to add the following statement to resolve the problem of the correct removal of cookies information:

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

The above for myself in the work of some small experience summary, may have inaccurate place, hope everybody approve is!
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.