title: How do you read user environment variables with ASP.NET Core 2.1 in the ConfigureServices method?
body: <p>I am currently spinning up an ASP.NET Core Web API (version 2.1) and am trying to read custom environment variables that I have set from my local machine. The environment variables I have set are for the AWS client ids, secrets, etc. I am trying to access these environment variables' values within the ConfigureServices method inside the Startup.cs class. However, the following returns 'null', no matter what I try.</p>
  
  <pre><code>var result = Environment.GetEnvironmentVariable("&lt;ENV_VARIABLE&gt;");
  </code></pre>
  
  <p>In order to provide more backgruond, I need to read some client ids and secrets and pass them to the relevant class' constructor. Now, in order to follow best practices, I am not including these IDs and secrets inside the appsettings.json file. Instead, I modified the ~./bash_profile file on my Mac and wrote the following:</p>
  
  <pre><code># Set the General AWS Variables
  export AWS_ACCESS_KEY_ID=&lt;&gt;
  export AWS_SECRET_ACCESS_KEY=&lt;&gt;
  export AWS_DEFAULT_REGION=&lt;&gt;
  
  # Set the AWS Cognito Variables
  export COGNITO_USER_POOL_ID=&lt;&gt;
  export COGNITO_USER_POOL_CLIENT_ID=&lt;&gt;
  export COGNITO_USER_POOL_CLIENT_SECRET=&lt;&gt;
  </code></pre>
  
  <p>I also verified that these environment variables persisted by running</p>
  
  <pre><code>printenv
  </code></pre>
  
  <p>Then, I made the ConfigureServices method inside the Startup.cs class look like the following:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  
      // The following 3 variables are null
      var userPoolId = Environment.GetEnvironmentVariable("COGNITO_USER_POOL_ID");
      var userPoolClientId = Environment.GetEnvironmentVariable("COGNITO_USER_POOL_CLIENT_ID");
      var userPoolClientSecret = Environment.GetEnvironmentVariable("COGNITO_USER_POOL_CLIENT_SECRET");
  
      var amazonCognitoIdentityProvider = new AmazonCognitoIdentityProviderClient(RegionEndpoint.USEast1);
      var cognitoUserPool = new CognitoUserPool(userPoolId, userPoolClientId, amazonCognitoIdentityProvider, userPoolClientSecret);
  
      services.AddSingleton&lt;IAmazonCognitoIdentityProvider&gt;(x =&gt; amazonCognitoIdentityProvider);
      services.AddSingleton&lt;CognitoUserPool&gt;(x =&gt; cognitoUserPool);
  
      services.AddCognitoIdentity();
  }
  </code></pre>
  
  <p>However, everytime, the return value of "Environment.GetEnvironmentVariable(...)" is 'null'. I'm not sure what I'm doing incorrectly. I assume that I have a misunderstanding of environment variables and/or how ASP.NET Core retrieves them but I would greatly appreciate some help as to why my current code does not read the environment variables.</p>
  
  <p>Thank you in advance!</p>
  
  <p>EDIT:</p>
  
  <p>I should also mention that I am trying to do this on a Mac. I'm not sure if that changes the approach on how to retrieve an environment variable, but I just want to mention it here in case it does!</p>