简体   繁体   中英

How to store and retrieve database connection password encrypted using C#

I have searched for an encryption method for storing and retrieving database username and password in web.config, but most of the posts are discussing only about creating a function to encrypt text but not storing as well as retrieving it.

Is there an inbuilt function to encrypt text in C# using a password phrase? I could not find any.

Now I use function given in one of the SO posts for encryption and decryption, but I see some special characters in the encrypted text. If I want to store the encrypted text in web.config the value should be html compliant.

I would be interested in any other approach for both storing and retrieval scenarios.

You can use Data Protection API to encrypt your data. That data could be decrypted only under current windows user account on this computer.

So, if you create a separate account for you web application pool, only web app would be able to encrypt and decrypt that data.

how about the encrypt/decrypt methods in this example

then

string conn = Decrypt(ConfigurationManager.ConnectionStrings[ConnString].ConnectionString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM