Hide password in c# source code

2019-07-27 15:59发布

问题:

I am using the following code in c# to download a file from my website:

WebClient webClient = new WebClient();
webClient.Credentials = new System.Net.NetworkCredential("username", "password");
webClient.DownloadFile("http://example.com/file.txt", "file.txt");

The file is only downloaded when certain criteria are met, so I don't want the users to be able to access the files on my site

My corcern is that if a curios users decompiles the code, he will find the password and be able to access all the files on my site.

I've read that a secure way to save the password is to store its hash, but I don't know how to implement it in this example.

What options do I have to keep my password secure and make it impossible for a user to find it?

回答1:

You simply don't. Users give you passwords to do stuff, not the other way around.

If the user has to prove "certain conditions", then pass proof of those certain conditions to the server, and let it decide whether to allow the download or not.



回答2:

A sobering reality: You can't protect information contained in your program like this.

A must-do: Choose a username/password that is only for accessing the special files this single program needs - not your "access my whole website" username and password.

But just know that all you are doing is adding a little bit of an obstacle, here; anyone who wants to can examine your program and find the username and password.

The only 'correct' way to do this is to do it based on the user's own credentials; their username and password within your own system, for example. Then you would need to give them access based on that information, and your program would need to prompt them for it.



回答3:

There is no way to prevent that. If you program is able to access the file under condition X, the user is able to trick the program into condition X and get the file no matter what. You can make it harder, but you can't make it impossible.



回答4:

If the data are in the program itself you can considered them as already being exposed to users. If the credentials are on the users computer regardless on how many measures you take to combat this there is always a possibility to find a way around it.

What you can do is implement a login form for your program and provide the users with login info. Then when the user enters the login info do a check on the server side if the credentials exist (usually by checking in a database) and if it matches send them the file.

But as always, there is the issue with users just sharing the login info with other people and so on.