سؤال

I'll start by saying, maybe this is overkill.

During my login routine, I encrypt the user's login id prior to using FormsAuthentication.SetAuthCookie.

The problem is that if the encrypted string ends up having escape characters, the string that get saved gets truncated do to the escape characters.

Should I just abondone trying to encrypt the user's login id?

Or, is there a way to get around this issue?

Here is a sample string that gets truncated: << *€ƒKõ‹¯Þ\0ý´Gv\"þEaÔs0n×\tD¦™s€7Œ>>

هل كانت مفيدة؟

المحلول

When you encrypt the user id, you should use Base64 encoding so that the encrypted data will only contain valid characters (alphanumeric, +, /, =).
You would probably find this helpful: Convert.ToBase64String(byte[])

Example:

string userId = "Hello";
byte[] encryptedData = GetEncryptedBytes(userId);
string encodedUserId = Convert.ToBase64String(encryptedData);
// encodedUserId is "SGVsbG8="

FormsAuthentication.SetAuthCookie(encryptedUserId);

And decoding is the reverse:

string encodedUserId = "SGVsbG8=";
byte[] encryptedData = Convert.FromBase64String(encodedUserId);
string userId = GetDecryptedString(encryptedData);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top