I am trying to configure some key/value pairs for my Azure web application using app settings section on Windows Azure preview portal.
Now I am trying to read values like below
ConfigurationManager.AppSettings["MyWebApp.DbConnectionString"];
but it returns null values.
Reading app settings from Web.config in my web application works fine.
I found the solution.
Keep values in web.config as well as in Azure App setting. When you are running/debugging application on your local environment it picks values from web.config.
When you deploy application on Azure it picks values from App setting.
//Below code work for both.
ConfigurationManager.AppSettings["KeyName"]
Keep key name same in web.config as well as in Azure app setting.
In Azure, there are a few different ways of retrieving Application Settings and Connection Strings. However, connection strings work a little differently than vanilla application settings.
Application Settings can be retrieved by any method, regardless of whether or not they are present in the Web.config file.
Connection Strings can also be retrieved by any method if the string is defined in Web.config. However, if the connection string is NOT defined in Web.config, then it can only be retrieved using the Environment Variable method.
Retrieving as Environment Variable
Environment.GetEnvironmentVariable("APPSETTING_my-setting-key");
Environment.GetEnvironmentVariable("SQLAZURECONNSTR_my-connection-string-key");
Note that the keys must be prepended with a string designating their type when using this method.
All Application Settings use the APPSETTING_ prefix.
Connection Strings have a different prefix depending on the type of database selected when creating the string in the portal:
"Sql Databases" --> "SQLAZURECONNSTR_my-connection-string-key"
"SQL Server" --> "SQLCONNSTR_my-connection-string-key"
"MySQL" --> "MYSQLCONNSTR_my-connection-string-key"
"Custom" --> "CUSTOMCONNSTR_my-connection-string-key"
For a full overview, see the Windows Azure Web Sites documentation.
System.Environment.GetEnvironmentVariable("SERVICEBUS_CONNECTION")
works great!
Related
We use WCF client in our project which is an Azure functions app, to communicate with an external web service. We need to change the xmlSerializer's tempFilesLocation because of the permission issue. I searched online and found the following configuration that we can use in our web.config which will solve the problem.
<system.xml.serialization>
<xmlSerializer tempFilesLocation="an absolute path of your choice"/>
</system.xml.serialization>
But in Azure Functions app, we don't have access to web.config, so we need to find a way to do it in the code. Is there any way to change tempFilesLocation in the code?
It's not possible to modify the web.config for functions running on the dynamic sku (where you pay-per-invocation).
However, if you create your function on the non-dynamic/classic sku (where you pay per vm, the pricing model for regular web apps) then you can modify the web.config settings via an applicationHost.xdt file. More details on how to work with xdt file here: https://github.com/projectkudu/kudu/wiki/Xdt-transform-samples
In an Azure Websites I was always using the following code to fetch some values from the config's app settings:
string property = WebConfigurationManager.AppSettings["property"];
Just a couple of days ago I stublemd upon CloudConfigurationManager, and with it I can get the property like so:
string property = CloudConfigurationManager.GetSetting("property");
Although CloudConfigurationManager seems like it's better fitted to cloud use, I never had any issues with WebConfigurationManager.
Should I be using CloudConfigurationManager?
What are the differences between the two?
In what cases CloudConfigurationManager will behave diffrent from
WebConfigurationManager?
CloudConfigurationManager enables us to read configuration file regardless of the environment we are in.
So instead of writing environment specific code statements e.g., for web.config file:
WebConfigurationManager.AppSettings["MySetting"]
For ServiceConfiguration.cscfg file:
RoleEnvironment.GetConfigurationSettingValue("MySetting")
We can write the below statement, which will read values from all the configuration files i.e., app.config, web.config and ServiceConfiguration.cscfg.
CloudConfigurationManager.GetSetting("MySetting")
CloudConfigurationManager requires Microsoft.WindowsAzure.Configuration assembly, part of Azure SDK or separate NuGet.
WebConfigurationManager requires System.Web.Configuration assembly, part of .NET Framework.
WebConfigurationManager and CloudConfigurationManager manage different configuration files.
WebConfigurationManager is for managing website's web.config file(s) and it's appsettings and connections strings
CloudConfigurationManager is for managing .cscfg files (for cloud services). His benefit is that you can manage configurations and connections from the azure portal directly.
I think you're better off using WebConfigurationManager.
With it, you have access to ConnectionStrings as well as AppSettings.
Both sets of settings you can update via the Azure Portal. They can then further be used in other Azure facilities/services, such as when configuring website backup.
Check this out for more information: https://azure.microsoft.com/en-us/blog/windows-azure-web-sites-how-application-strings-and-connection-strings-work/
I'm developing a .NET Framework 4.0 based Windows application.
I have a requirement of distributing this window application, along with source code to client.
When I test I'm using my own database credentials.
So I want a method to somehow hide the app.config details.
For this, I tried with encrypting values in app.config but faced an issue with token keys.
While researching about it, I found that I can use:
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration.
But that again required username and password for accessing remote server and don't want to show them to the client.
So for this I planned to read web.config hosted on IIS 7.5 Server.
Could you please help me in that context?
Or if you have better ideas to achieve the objective, do share.
The code is designed to be very close in syntax to the usual method used for accessing web.config from a web app. Pass the constructor the location of the web.config file you wish to parse and then use the AppSettings method to obtain the desired value;
string filename = #"c:\temp\Web.Config";
UK.Org.Webman.ConfigurationSettings ConfigurationSettings =
new UK.Org.Webman.ConfigurationSettings(filename);
string PrimaryDatabase = ConfigurationSettings.AppSettings["PrimaryDatabase"];
I am having the following problem. I have an ASP.NET (VB.NET) application that is trying to connect to Amazon Web services using the AWS .NET SDK, and in order to do that the user is entering their Access and secret key. I am then adding the key to configuration like so:
Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration("~")
Dim settings As KeyValueConfigurationCollection = config.AppSettings.Settings
settings("AWSAccessKey").Value = AmazonAccessKeyText.Text
settings("AWSSecretKey").Value = AmazonSecretKeyText.Text
config.Save(ConfigurationSaveMode.Modified)
ConfigurationManager.RefreshSection("appSettings")
While this successfully modifies the web.config file, it still throws an exception at runtime saying that it can't find the Access key. Can anyone tell me what I am doing wrong?
If I understand correctly you have as ASP.NET application that connects to the AWS on the clients behalf, if so storing the clients credentials locally might be the best idea from a security point of view.
You can either
Only store the credentials in memory in the session only when needed in something like a SecureString
Store them encrypted on the disk using any crypt apis.
The amazon sdk doesn't need the credentials stored in the app/web.config. If you look at something like AWSClientFactory.CreateAmazonS3Client , you will notice that you can pass them as parameters to this function
Probably, it is better to keep your received in key in Application object? for example static.
It is much clearer and simpler and will not cause a recycle of application when web.config is modified
What is the best practice to store application settings (such as user name and password, database location ...) in C# ?
Hint: I am new to .net and C#
Application Configuration Settings that are application wide (non-user specific) belong in either app.config (for Desktop apps) or web.config (for Web apps).
Encrypting sections of a web.config file is quite simple as outlined in this Super Simple Example.
If you need to store User specific settings (like application settings, etc.) or Application wide settings not related to application configuration you can use a Settings file as described here:
User Settings in C#
I'm not sure what version of .net/Visual Studio it was introduced in, but you can right click on your project, choose 'Add New Item' and select 'Settings File' from the "Add New Item" window. This provides your project with a (named by default) Settings.settings file that you can configure all the settings you want to expose in.
You can define settings that you create to be either Application or User which means you can use this single interface to control global and user settings. Once you've created a setting in the Settings.settings file using the editor that Visual Studio provides, you can access it in code like this:
// Get a Setting value
var valueOfSetting1 = Settings1.Default.Setting1;
// Modify and save a Setting value
Settings1.Default.Setting1 = "New Value";
Settings1.Default.Save();
First option is the registry. It is easy, but it is not too safe for passwords. Another option is using a file that you create. This too isn't safe, unless you want to implement cryption.
Next option is using the Application Settings. This is also quite simple, but there are a few catches. First, right click on your project and go to Properties. There, under the Settings tab, you can store variables to which you can access from your program by
string password = Properties.Settings.Default.Password
You can also change them the same way, but ONLY IF the scope is set the User. WHen the scope is application-wide, VS does not allow you to change these variables for some odd reason. To save changes, you must call Save() as follows:
Properties.Settings.Default.Save();
These are saved in the User Data folder under C:\Documents and Settings\'Current User'\Local Settings\Application Data\
Another option would be to include them in your database, but since you are also storing your database location, this might not work for you.
I think app.config (non web app) or web.config (web app).
These sorts of settings usually land in Application Configuration Files (web.config, app.config).
http://www.devasp.net/net/articles/display/679.html
If you are storing passwords, you might also need to encrypt the configuration section in question.
http://msdn.microsoft.com/en-us/library/53tyfkaw.aspx
http://msdn.microsoft.com/en-us/library/ff650304.aspx
Note if you use app.config, you will see it get renamed to ..config, depending on if your output produces a DLL or an EXE.
As with the above replies suggest, app.config or the web.config is the best place for app settings.
If you need a more robust way of xml style tags for database, server settings and the like, you can use the configurationSection and create custom sections.
http://www.codeproject.com/KB/cs/CustomConfigurationSectio.aspx
For database passwords, the way i do it is have an encrypted string in the xml tag value and decrypt then when reading them, that way you dont expose the passwords.
appsettings config file, ini file(nini), embeddable database(sqlite,berkelydb/etc..),whatever method you like, it depends on your application size/performance consideration and design.