1

I'm new to ASP.NET Core. I am trying to develop a simple ASP.NET Core Web API. The API connects to the database server in the cloud. I stored the connection string in the appsettings.json, deployed the API to my local IIS, and ran the service. It worked fine. When I try to move the connection string to the environment variables and deploy again, the Web API does not work. The browser shows me "The page isn't working. HTTP error 500".

I have couple of questions to ask:

  1. In a real world application, where should we store the connection string and deploy to the server since the user name and password is sensitive?
  2. What's wrong with my Web API, since it wont work when the connection string is stored in the environment variables?

Thanks

12
  • You may want to temporarily enable UseDeveloperExceptionPage() on the server so that you can get a more specific error message. Alternatively, you may be able to get exception details from your cloud host’s logs. Commented Jan 8, 2020 at 6:31
  • What cloud hosting service are you using? That may impact the recommended location for storing your secrets. Commented Jan 8, 2020 at 6:32
  • I deploy the API in my local machine IIS. The database is sit in the AWS. It's work fine when the connectionstring is in appsetting.json while in Environment variable, it not work. Commented Jan 8, 2020 at 6:35
  • Ah, I see that now regarding your localhost. I apologize for missing that previously. Regardless, enabling UseDeveloperExceptionPage() in your Startup class should provide you with a more specific exception message which will aid in isolating the issue. Commented Jan 8, 2020 at 6:41
  • I try to run the web API in visual studio, when hit the Environment.GetEnvironmentVariable("Connection"), it give me an error message "Value cannot be null.Parameter name: connectionString' ". What's means? In my Environment Variable, i set the key as ConnectionStrings : Connection. Commented Jan 8, 2020 at 6:47

1 Answer 1

0

You should try this

In Program.cs

WebHost
.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(
    (hostingContext, builder) =>
    {
        var environment = hostingContext.HostingEnvironment;
        var configuration = new ConfigurationBuilder()
            .SetBasePath(System.IO.Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true, reloadOnChange: true)
            .Build();
        //codes... 
    })
.UseStartup<Startup>()
.Build()
.Run();

and your file names should be like appsettings.Development.json, appsettings.Staging.json, ...

I answer your first question: If you want protect your username and password

  1. You should use third-party applications (like consul)

  2. You hash your configuration file. But this method does not make sense.

  3. You do write and use hard code. But not change except deploy.

Sign up to request clarification or add additional context in comments.

9 Comments

May I know what the purpose of those file appsettings.Development.json, appsettings.Staging.json?
These files configure the system according to the state of the environment. For example if you work development mode, the system uses appsettings.Development.json and appsttings.json files. (see more Continuous Integration)
My Environment variable is set to development. If I want add connection string to json file, which one I should add to?
If your connection string change in a different environment, you should write this to environment-specific config.
I add connectionstring in appsettings.json while the environment set to development. It still able to connect to database. It's true my setting?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.