문제

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