简体   繁体   English

将XML数据存储在Cookie中

[英]Storing xml data in a cookie

I'm trying to store an xml serialized object in a cookie, but i get an error like this: 我正在尝试将XML序列化的对象存储在cookie中,但出现这样的错误:

A potentially dangerous Request.Cookies value was detected from the client (KundeContextCookie="<?xml version="1.0" ...")

I know the problem from similiar cases when you try to store something that looks like javascript code in a form input field. 当您尝试将看起来像javascript代码的内容存储在表单输入字段中时,我从类似的情况中知道了问题。

What is the best practise here? 最佳做法是什么? Is there a way (like the form problem i described) to supress this warning from the asp.net framework, or should i JSON serialize instead or perhaps should i binary serialize it? 有没有办法(如我描述的形式问题)抑制来自asp.net框架的此警告,或者我应该将JSON序列化,还是应该对它进行二进制序列化? What is common practise when storing serialized data in a cookie? 在Cookie中存储序列化数据时的常见做法是什么?

EDIT: Thanks for the feedback. 编辑:感谢您的反馈。 The reason i want to store more data in the cookie than the ID is because the object i really need takes about 2 seconds to retreive from a service i have no control over. 我想在Cookie中存储比ID多的数据的原因是,我真正需要的对象大约需要2秒钟才能退出我无法控制的服务。 I made a lightweight object 'KundeContext' to hold a few of the properties from the full object, but these are used 90% of the time. 我制作了一个轻量级对象“ KundeContext”来容纳完整对象中的一些属性,但是90%的时间都使用了这些属性。 This way i only have to call the slow service on 10% of my pages. 这样,我只需要在10%的页面上调用慢速服务即可。 If i only stored the Id i would still have to call the service on almost all my pages. 如果我仅存储ID,则几乎仍要在我的所有页面上调用该服务。

I could store all the strings and ints seperately but the object has other lightweight objects like 'contactinformation' and 'address' that would be tedious to manually store for each of their properties. 我可以分别存储所有字符串和整数,但是该对象还有其他轻量级对象,例如“ contactinformation”和“ address”,要为它们的每个属性手动存储会很繁琐。

Storing serialized data in a cookie is a very, very bad idea. 在cookie中存储序列化数据是一个非常非常糟糕的主意。 Since users have complete control over cookie data, it's just too easy for them to use this mechanism to feed you malicious data. 由于用户可以完全控制cookie数据,因此使用这种机制向您提供恶意数据实在太容易了。 In other words: any weakness in your deserialization code becomes instantly exploitable (or at least a way to crash something). 换句话说:反序列化代码中的任何弱点都可以立即被利用(或者至少是崩溃的一种方式)。

Instead, only keep the simplest identifier possible in your cookies, of a type of which the format can easily be validated (for example, a GUID). 取而代之的是,仅在Cookie中保留最简单的标识符,该标识符的格式易于验证(例如GUID)。 Then, store your serialized data server-side (in a database, XML file on the filesystem, or whatever) and retrieve it using that identifier. 然后,将序列化的数据存储在服务器端(在数据库中,文件系统上的XML文件或任何其他文件中),然后使用该标识符进行检索。

Edit: also, in this scenario, make sure that your identifier is random enough to make it infeasible for users to guess each other's identifiers, and impersonate each other by simply changing their own identifier a bit. 编辑:同样,在这种情况下,请确保您的标识符足够随机,以使用户无法猜测彼此的标识符,并且只需简单地更改自己的标识符就可以互相模仿。 Again, GUIDs (or ASP.NET session identifiers) work very well for this purpose. 同样,GUID(或ASP.NET会话标识符)可以很好地实现此目的。

Second edit after scenario clarification by question owner: why use your own cookies at all in this case? 问题负责人澄清场景后的第二次编辑:为什么在这种情况下完全使用您自己的cookie? If you keep a reference to either the original object or your lightweight object in the session state (Session object), ASP.NET will take care of all implementation details for you in a pretty efficient way. 如果您在会话状态下保留对原始对象或轻型对象的引用(会话对象),则ASP.NET将以一种非常有效的方式为您处理所有实现细节。

I wouldn't store data in XML in the cookie - there is a limit on cookie size for starters (used to be 4K for all headers including the cookie). 我不会在XML中将数据存储在cookie中-入门者的cookie大小是有限制的(对于包括cookie在内的所有标头,过去的大小都是4K)。 Pick a less verbose encoding strategy such as delimiters instead eg a|b|c or separate cookie values. 选择不太详细的编码策略,例如定界符,例如a | b | c或单独的cookie值。 Delimited encoding makes it especially easy and fast to decode the values. 分隔编码使解码值变得特别容易和快速。

The error you see is ASP.NET complaining that the headers look like an XSS attack. 您看到的错误是ASP.NET抱怨标头看起来像XSS攻击。

Look into the View State . 查看“ 查看状态” Perhaps you'd like to persist the data across post-backs in the ViewState instead of using cookies. 也许您想在ViewState中跨回发保留数据,而不是使用cookie。 Otherwise, you should probably store the XML on the server and a unique identifier to that data in the cookie, instead. 否则,您可能应该将XML存储在服务器上,并将该数据的唯一标识符存储在cookie中。

You might look into using Session State to store the value. 您可能会考虑使用会话状态来存储值。 You can configure it to use a cookie to store the session id. 您可以将其配置为使用Cookie来存储会话ID。 This is also more secure, because the value is neither visible or changeable by the user-side. 这也更加安全,因为该值在用户端既不可见也不可更改。

Another alternative is to use a distributed caching mechanism to store the value. 另一种选择是使用分布式缓存机制来存储值。 My current favorite is Memcached . 我目前最喜欢的是Memcached

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM