问题
I'm working on a windows desktop program. This program will connect to a remote SQL server. It has a 3-tier architecture. So the Solution contains several ClassLibraries as well as a main Windows Form Application. I don't feel safe using connection string like this:
string connStr = "Data Source=94.xx.xxx.xx; Initial Catalog=xxxxx; User Id=xxxxx; Password=xxxxx";
Is it safe to use like that ? Is it possible for someone to decompile the ".exe" file and access this connection string? I know I can use app.config file. But I was failed to import "ConfigurationManager" and couldn't use "ConfigurationManager.ConnectionStrings["xxx"].ConnectionString.ToString();" code. Besides it doesn't feel safe too. I think, the best practice would be to encrypt the connection string. I found an example for that :
http://www.codeproject.com/Articles/18558/Encrypting-windows-application-connection-strings
But it uses an older version of Visual Studio. And I couldn't figure out how to add a "Setup Project with a custom action containing the project's primary output" .
Is there any other way to safely use connection string in a 3-tier architectured Windows forms application?
PS: I'm using Visual Studio Express 2013 for Windows Desktop
回答1:
It is not possible to hide a connection string on a machine. Any advice to the contrary is snake oil. Encryption will not help. All possible schemes you come up will be (easily) defeated by a local administrator on the machine that runs your application. a local administrator will always have access to your local encryption keys, no matter how fancy you try to design your protection.
Deploying application that connect to a public SQL Server using embedded name and password is a doomed exercise in futility, no matter how you try to hide the name and password.
A better stance is to deploy an application that connects using a name and password entered by the user (a login dialog). Obviously each application user uses a different SQL user/password. But this also crumbles to pieces on any medium sized deployment as the name/password maintenance for each user becomes impossible to manage.
A decent solution is to use integrated authentication, but that only works within a domain (ie. a corporation). If your application is to be distributed within a domain (or a forest), ie. in a corporation, than integrated authentication is the proper way to do it.
If you distribute your application to public at large and your application needs to 'phone home' and connect to a SQL Server over public internet, then you need to go back to the drawing board. It will never work, the security problem is impossible to solve. You'll have to change your application connect to a service interface (REST, SOAP) and authenticate to it via your preferred authentication method (forms, oauth). Needless to say, if you coded your app using direct SQL connectivity (EF, Linq, ADO.NEt) it all goes down the drain and you have to start a new app from scratch.
回答2:
You have to open VS Command Prompt and write below mentioned command.
for encryption
aspnet_regiis -pe connectionStrings -app /VirtualDirectioryPath -prov DataProtectionConfigurationProvider
or
ASPNET_REGIIS -pef "connectionStrings" "D:\IIS\admin.mySite.com"
For Decryption
aspnet_regiis -pd connectionStrings -app /VirtualDirectioryPath
or
ASPNET_REGIIS -pdf "connectionStrings" "D:\IIS\admin.mySite.com"
来源:https://stackoverflow.com/questions/24506094/encrypt-connection-strings