I have added the following settings inside my web.config file to initiate an API call to external system. So I am storing the API URL + username + password as follows:-
<appSettings> <add key="ApiURL" value="https://...../servlets/AssetServlet" /> <add key="ApiUserName" value="tmsservice" /> <add key="ApiPassword" value="test2test2" />
Then inside my action method I will be referencing these values when building the web client as follows:-
public ActionResult Create(RackJoin rj, FormCollection formValues) { XmlDocument doc = new XmlDocument(); using (var client = new WebClient()) { var query = HttpUtility.ParseQueryString(string.Empty); foreach (string key in formValues) { query[key] = this.Request.Form[key]; } query["username"] = System.Web.Configuration.WebConfigurationManager.AppSettings["ApiUserName"]; query["password"] = System.Web.Configuration.WebConfigurationManager.AppSettings["ApiPassword"]; string apiurl = System.Web.Configuration.WebConfigurationManager.AppSettings["ApiURL"];
But in this was I will be exposing the username and password and these can be captured by users, so my question is how I can secure the API username and password?
Storing a password in the registry does not secure it. Anyone with access to the server can read it there.
Encrypting a Web Configuration Section To encrypt configuration file contents, use the Aspnet_regiis.exe tool with the –pe option and the name of the configuration element to be encrypted. Use the –app option to identify the application for which the Web.
1) There is always a chance that somehow a hacker manages to read your web. config in one way or another. If a hacker manages to compromise your web server, having your web. config encrypted would at least slow down, if not stop a hacker from gaining access to your database.
You can encrypt the web.config with aspnet_regiis. This is to stop people with access to your server from reading sensitive information.
By the way, I would put your config settings inside a class, that can then be injected into your controllers - it will make unit testing easier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With