I am working on a asp.net mvc web application that perform some API calls to other web applications. but currently I am storing the API username, API password and API URL inside my code. As follow:-
using (var client = new WebClient())
{
// client.Headers[HttpRequestHeader.Accept] = "AddAsset";
var query = HttpUtility.ParseQueryString(string.Empty);
foreach (string key in formValues)
{
query[key] = this.Request.Form[key];
}
query["username"] = "testuser";
query["password"] = "……";
query["assetType"] = "Rack";
query["operation"] = "AddAsset";
var url = new UriBuilder("http://win-spdev:8400/servlets/AssetServlet");
url.Query = query.ToString();
try
{
But storing these setting inside my code will not be flexible in case I need to change the API call settings, and it is not secure. So what is the best way to store these setting . I was thinking to create two database tables one for storing the URL(and there might be multiple URLs sin the future). And another table to store the username and password. So is creating database tables the right way to store these settings.
web. config file is an XML-based configuration file used in ASP. NET-based applications to manage various settings that are concerned with the configuration of our website.
There is no restriction to use the web. config file in the asp.net web application. You can have 1 Web. config file per folder .
Locate the web. config file in the root directory of your application (or create one if it does not already exist). Add an <appSettings> element. Add <add> child elements along with key / value pairs to the <appSettings> element as required.
You should use the Web.Config file (Configuration API) to store these settings. This way you will be able to update settings without having to recompile your entire application every time. That's the most standard way to store settings in a Web (MVC or WebForms) application, and gives you the possibility to encrypt sensitive settings transparently to your application.
You can access stored settings using the WebConfigurationManager class. Here you can find some examples of how to use it.
Code sample:
Web.config appSettings:
<appSettings>
<add key="ApiUserName" value="MySampleUsername" />
</appSettings>
C# Code:
string userName = System.Web.Configuration.WebConfigurationManager.AppSettings["ApiUserName"];
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