2011-11-29 51 views
2

我會開始說,也許這是矯枉過正。使用FormsAuthentication.SetAuthCookie存儲加密字符串

在我的登錄例程中,我在使用FormsAuthentication.SetAuthCookie之前加密用戶的登錄ID。

問題是,如果加密的字符串結束有轉義字符,獲取保存的字符串將被截斷做轉義字符。

我應該只是abondone試圖加密用戶的登錄ID?

或者,有沒有辦法解決這個問題?

這裏是一個被截斷的樣本串: < < *€ƒKõ>

回答

2

當你加密的用戶ID,你應該使用Base64 encoding,使得加密後的數據將只包含有效的字符(字母和數字,+/=
你可能會有所幫助:Convert.ToBase64String(byte[])

例子:

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

FormsAuthentication.SetAuthCookie(encryptedUserId); 

和解碼相反:

string encodedUserId = "SGVsbG8="; 
byte[] encryptedData = Convert.FromBase64String(encodedUserId); 
string userId = GetDecryptedString(encryptedData); 
+0

將如何影響我,當我嘗試解密字符串? – SLoret

+0

答覆已更新。 –

+0

謝謝,我會給它一個鏡頭。 – SLoret