How to encode cookies

  • Thread starter Thread starter Peter Afonin
  • Start date Start date
P

Peter Afonin

Hello:

I'm using cookies in my application. I store them like this:

Response.Cookies("Cookie_FullName").Value = customerDetails.FullName

Then I retrieve them like this:

Request.Cookies("Cookie_FullName").Value

If the name is in English - everything works OK. But when the name is in,
for instance, Cyrillic - it comes out as garbage.

Is there any way to encode the cookie, for instance, in Unicode or Cyrillic
before storing it?

I would appreciate your help.

Thank you,
 
Hi, Peter,

You can store the value like this:

Dim cyrillicString As String = "??????"
Response.Cookies("CookieName").Value = _
Convert.ToBase64String( _
System.Text.Encoding.Unicode.GetBytes(cyrillicString))

Then store the inBase64 string in the cookie. The reverse conversion:

Dim cookieValue As String = _
System.Text.Encoding.Unicode.GetString( _
Convert.FromBase64String( _
Request.Cookies("CookieName").Value))

Hope this helps
Martin
 
Thank you very much, Martin. It worked.

Peter

Martin Dechev said:
Hi, Peter,

You can store the value like this:

Dim cyrillicString As String = "??????"
Response.Cookies("CookieName").Value = _
Convert.ToBase64String( _
System.Text.Encoding.Unicode.GetBytes(cyrillicString))

Then store the inBase64 string in the cookie. The reverse conversion:

Dim cookieValue As String = _
System.Text.Encoding.Unicode.GetString( _
Convert.FromBase64String( _
Request.Cookies("CookieName").Value))

Hope this helps
Martin
 
Back
Top