views:

140

answers:

2

I have an application that stores each user role as a comma-delimited string in the userData portion of the forms authentication ticket.

We have run into a situation where a user was part of many roles, and those roles all have long names. There is a maximum # of characters that you can put into the userData, likely a limitation of cookies.

So, is there a low-cost way I can shorten this string and 'rehydrate' it later?

Unforunately, the userData portion of the ticket is a string - if it were an object I could store it as byte[] and use System.Text.Encoding.Unicode.GetString() later.

I guess this is really a question about being able to shorten a string, store it as a string, and rehydrate it later without losing its original value.

+4  A: 

How about just using the roleId ? If setup properly you could even OR these together to form a single integer containing all the roles.

leppie
That would be my answer, though that would probably affect quite a bit of his other code.
Jeremy Powell
+1 Bitwise is very appropriate for this situation assuming roleids are mutually exclusive.
statenjason
Correct, that would affect a bunch of code.
ScottE
We use it exactly same way "1:12" where 1 represents role and 12 represents user id and it can go for years to pass the max limit. There is no shorter method because in order to use compression you should have good amount of repetation in your string.
Akash Kava
+3  A: 

So, string compression?

I'd start by looking at System.IO.Compression

http://msdn.microsoft.com/en-us/library/system.io.compression.aspx

Chad
Though, with short strings, <500, characters it'll probably be hard to find a way to compress it, and actually be of a benefit.
Chad
This doesn't sound very low-cost.
ScottE
+1, I just did some arbitrary testing with text data from 500 bytes to 4096 bytes (the max in IE for cookie size), using gzip and then base64, and still got a %10-%40 size decrease. Not bad.
orip
Did your test data look like @ScottE's? If it was just all 'a's then it definitely will produce those results. :)
Jeremy Powell