If you want to set a cookie with multiple values ββor "subkeys" and read them with .NET, you need to save the subkey as name and name pairs, formatted as a query string. You can use the jQuery.param () method to convert a Javascript object to a query string.
var obj = { email: 'jdoe@example.com', username: 'jdoe' }; $.cookie("MyTestCookie", $.param(obj), { expires: 10 });
Then on the server you can access the values ββas:
var email = Request.Cookies["MyTestCookie"]["email"]; var username = Request.Cookies["MyTestCookie"]["username"];
EDIT: I created a test page to show reading / writing multicurrency cookies on both the server and client. http://www.systemex.net/Cookies/
NOTES:
- You need to take care of shielding and disconnecting the connections. Thus, any inline = and and are handled correctly
- When reading and writing jQuery cookies, use the {raw: true} parameter so that it does not double the escape code.
- I wrote a function $ .deparam that converts the string name = value & name2 = value2 to a javascript object {name: value, name2: value2}
- Recently, the jquery cookie plugin does not overwrite a cookie with the same name, it just adds it to the current cookie collection. At this point, it would probably be better to rewrite the plugin to support plugins and modify existing cookies.
Anyway, hope this helps.
Here is Default.aspx
<h1>Get Cookie From Server:</h1> <ul> <li>Email: <%= GetCookie("MyTestCookie", "email")%></li> <li>Username: <%= GetCookie("MyTestCookie", "username")%></li> </ul> <h1>Get Cookie From Client:</h1> <ul> <li>Email: <span class="cookie" data-name="MyTestCookie" data-key="email" /></li> <li>Username: <span class="cookie" data-name="MyTestCookie" data-key="username" /></li> <li>Raw: <span id="raw" /></li> </ul> <h1>Set Cookie From Client:</h1> <ul> <li>Email: <input type="text" name="email" value="" /></li> <li>Username: <input type="text" name="username" value="" /></li> </ul> <input type="submit" value="Submit" /> <script> $(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('&'); for (var i = 0; i < a.length; i++) { var pair = a[i].split('='); o[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]); } return o; } </script>
Default.aspx.cs
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&123"); Response.Cookies.Add(cookie); } } public string GetCookie(string name, string key) { var cookie = Request.Cookies[name]; return cookie != null ? HttpUtility.UrlDecode(cookie[key]) : ""; }
Kiliman
source share