Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to set a cookie that has multiple values or "subkeys" and have them readable from .NET, you need to store the subkey as name-value pairs formatted like a querystring. You can use the jQuery.param() method to convert a Javascript object into a querystring.</p> <pre><code>var obj = { email: 'jdoe@example.com', username: 'jdoe' }; $.cookie("MyTestCookie", $.param(obj), { expires: 10 }); </code></pre> <p>Then on the server, you can access the values as:</p> <pre><code>var email = Request.Cookies["MyTestCookie"]["email"]; var username = Request.Cookies["MyTestCookie"]["username"]; </code></pre> <p>EDIT: I created a test page to show reading/writing multi-value cookies both on the server and client. <a href="http://www.systemex.net/Cookies/">http://www.systemex.net/Cookies/</a></p> <p>NOTES:</p> <ul> <li>You need to take care of escaping and unescaping the subkeys. This way any embedded = and &amp; are handled correctly</li> <li>When reading and writing jquery cookies, use option { raw: true } so it doesn't double escape.</li> <li>I wrote a $.deparam function that will convert a name=value&amp;name2=value2 string into a javascript object { name: value, name2: value2 }</li> <li>One last thing, the jquery cookie plugin does not overwrite a cookie with the same name, it simply appends it to the current cookie collection. At this point, it would probably be better to rewrite the plugin to support subkeys and modifying existing cookies.</li> </ul> <p>Anyway hope this helps.</p> <p>Here is Default.aspx</p> <pre><code>&lt;h1&gt;Get Cookie From Server:&lt;/h1&gt; &lt;ul&gt; &lt;li&gt;Email: &lt;%= GetCookie("MyTestCookie", "email")%&gt;&lt;/li&gt; &lt;li&gt;Username: &lt;%= GetCookie("MyTestCookie", "username")%&gt;&lt;/li&gt; &lt;/ul&gt; &lt;h1&gt;Get Cookie From Client:&lt;/h1&gt; &lt;ul&gt; &lt;li&gt;Email: &lt;span class="cookie" data-name="MyTestCookie" data-key="email" /&gt;&lt;/li&gt; &lt;li&gt;Username: &lt;span class="cookie" data-name="MyTestCookie" data-key="username" /&gt;&lt;/li&gt; &lt;li&gt;Raw: &lt;span id="raw" /&gt;&lt;/li&gt; &lt;/ul&gt; &lt;h1&gt;Set Cookie From Client:&lt;/h1&gt; &lt;ul&gt; &lt;li&gt;Email: &lt;input type="text" name="email" value="" /&gt;&lt;/li&gt; &lt;li&gt;Username: &lt;input type="text" name="username" value="" /&gt;&lt;/li&gt; &lt;/ul&gt; &lt;input type="submit" value="Submit" /&gt; &lt;script&gt; $(function () { $(".cookie").each(function (index) { var name = $(this).data('name'); var key = $(this).data('key'); var cookie = $.deparam($.cookie(name, { raw: true })); $(this).html(cookie[key]); }); $('#raw').text(escape($.cookie("MyTestCookie"), { raw: true })); $("form").submit(function () { var o = {}; o.email = $('input[name=email]').val(); o.username = $('input[name=username]').val(); var value = $.param(o); var cookie = $.cookie('MyTestCookie', value, { raw: true }); return true; }); }); jQuery.deparam = function (params) { var o = {}; if (!params) return o; var a = params.split('&amp;'); for (var i = 0; i &lt; a.length; i++) { var pair = a[i].split('='); o[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]); } return o; } &lt;/script&gt; </code></pre> <p>Default.aspx.cs</p> <pre><code>protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { var cookie = new HttpCookie("MyTestCookie"); cookie["email"] = HttpUtility.UrlEncode("jdoe@example.com"); cookie["username"] = HttpUtility.UrlEncode("jdoe&amp;123"); Response.Cookies.Add(cookie); } } public string GetCookie(string name, string key) { var cookie = Request.Cookies[name]; return cookie != null ? HttpUtility.UrlDecode(cookie[key]) : ""; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload