2015-09-11 17 views
1

如果字符串被用作參數,它還在內存中嗎?如果使用字符串作爲參數,字符串是否仍然在內存周圍浮動?

例如,

OracleConnection conn = new OracleConnection("userid=ididid; userpassword=pwpwpw"); 

是否有違反安全的潛在威脅?

不知GC是如何工作的上被用作唯一的參數字符串...

+1

http://stackoverflow.com/questions/26190938/is-securestring-ever-practical-in-ac-sharp-application – Habib

回答

1

還有一點,你可以用隨附的字符串常量字符串做的事:他們是interned,這樣他們就不會被垃圾收集,直到你的程序退出。

請注意,在這種情況下使用SecureString將無濟於事,因爲如果將字符串文本的內容複製到SecureString的實例中,則字符串文字將保留在程序的圖像中。

在另一方面,當你的字符串的源是文字或常量,你就可以防止您的字符串的內容保留在內存的時間比你需要它:

OracleConnection conn; 
using (var pwd = new SecureString()) { 
    pwd.Append(...); // Append characters of the password to the string 
    ... // Append more characters... 
    conn = new OracleConnection(pwd); 
} 
// At this point pwd is erased from memory 
相關問題