2016-03-02 21 views
0

我在我的應用程序配置中有一個SecureString,但是在重試它時,我無法將字符串重鑄到SecureString。C#app.config SecureString字符串不能重鑄爲SecureString

string UserPassword = AppSetting.Get(config, "password"); 
UserCredential.Password = (SecureString)UserPassword; 

無法將類型'字符串'轉換爲'System.Security.SecureString'。

任何幫助將是偉大的。

謝謝。

+2

我想你誤會一個SecureString的做什麼。未加密時,app.config包含明文字符串。 ConfigurationManager將配置值作爲普通字符串處理。 SecureString所做的一切都是確保它所持有的字符串不會連續地放在內存中......使用AppSettings時,以某種方式,字符串在某些時刻以明文形式存在於內存中。你能退後一步並解釋你想要做什麼嗎? – CodeCaster

+0

謝謝CodeCaster。我在辦公室分發應用程序,需要登錄到一些特定的系統,所以我需要加密應用程序配置中使用的密碼。 –

+0

@CodeCaster對不起,這裏發佈Q ....它被禁用我.....我有一個應用程序讀取SecureString。這是工作在除了一個客戶端的價值是返回空......無論任何想法,如果有一些設置在勝利這樣做嗎? apprecatiate你的迴應!謝謝 – Taran

回答

0

你必須在發送每char建立一個安全字符串:

var secure = new SecureString(); 
foreach(var c in UserPassword) 
{ 
    secure.AppendChar(c); 
} 

UserCredential.Password = secure; 
+0

UserPassword已經是app.config –

+0

@Lee中的SecureString,這是毫無意義的。你是怎麼做到的? – CodeCaster

+0

嗨CodeCaster,我已經保存密碼在app.config已經作爲一個SecureString,但我有問題檢索它。 –