在BASE64饼干编码不能正确地发送到服务器 [英] Cookie encoding in BASE64 cannot be sent correctly to server

查看:255
本文介绍了在BASE64饼干编码不能正确地发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的 BASE64 的为en code的 GUID 的值,并把它们添加到饼干。例如,一封codeD GUID值 vClFwpDbWE6JPUlnlBXMWg == 。当服务器发送的响应,这将增加该cookie。我请与浏览器,此值正确地浏览器接收。但是,当浏览器发送另一个请求,该cookie值从Htt的prequestMessage的Cookie,为什么有些字符被删除更改为 vClFwpDbWE6JPUlnlBXMWg

I use BASE64 to encode GUID value and add them to cookie. For example, an ecoded guid value is vClFwpDbWE6JPUlnlBXMWg==. When the server sends response, it will add this cookie. I check with Chrome, this value is correctly received by the browser. But when the browser sends another request, the cookie value is changed to "vClFwpDbWE6JPUlnlBXMWg" from HttpRequestMessage's cookies, why some characters are removed?

我用WebAPI2,MVC5与IIS7.5。

I use WebAPI2, MVC5 with IIS7.5.

推荐答案

ASP.NET看到cookie中的'='字符,并假定它是一个多值的cookie(请参阅相关的问题的Storing~~V在饼干多个值)。

ASP.NET sees the '=' character in the cookie and assumes it's a multi-value cookie (see related question Storing multiple values in cookies).

您最好的选择是对的GUID存储在cookie原样,例如,通过使用Guid.ToString()打开GUID成十六进制字符串和新的GUID(字符串)转十六进制字符串回到一个GUID 。或者,如果你真的需要凝结下来为Base64,可以考虑使用HttpServerUtility的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.urltokenen$c$c(v=vs.110).aspx\"相对=nofollow> UrlTokenEn code 和<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.urltokende$c$c(v=vs.110).aspx\"相对=nofollow> UrlTokenDe code 方法。这些方法使用的编码,这是非常相似BASE64但不使用像+和=哪些字符由ASP.NET特殊处理

Your best bet is to store the GUID in the cookie as-is, e.g., by using Guid.ToString() to turn the GUID into a hex string and new Guid(string) to turn the hex string back into a GUID. Alternatively, if you really need to condense it down to BASE64, consider using HttpServerUtility's UrlTokenEncode and UrlTokenDecode methods. Those methods use an encoding which is very similar to BASE64 but which doesn't use characters like '+' and '=' which are treated specially by ASP.NET.

这篇关于在BASE64饼干编码不能正确地发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆