0

We are trying to read environment variables in our .NET Core 3 web service.

If I run this service locally I can debug the builder.Configuration and it shows all the variables regardless if they are set as system or user variables. But, after deployment to our server, it no longer sees ANY of them, no matter if they are set as user or system environment variables.

We really desire to set them under the User context of the Service Account that the web app is running under and need this to work as no person logging into the system would be able to view them unless they had logged in using the service account credentials.

var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

builder.Configuration
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile($"appsettings.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings.{env}.json", optional: false, reloadOnChange: true)
    .AddEnvironmentVariables(prefix: env);

Log.Debug($"Reading environment variable {env}-adm.s_id--" + builder.Configuration[env + "-adm.s_id"]);
Log.Debug($"Reading environment variable {env}-enc.s_id--"+ builder.Configuration[env + "-enc.s_id"]);
Log.Debug($"Reading environment variable {env}-srv.s_id--"+ builder.Configuration[env + "-srv.s_id"]);
Log.Debug($"Reading environment variable {env}-sett.s_id--" + builder.Configuration[env + "-sett.s_id"]);
Log.Debug($"Reading environment variable {env}-trans.s_id--" + builder.Configuration[env + "-trans.s_id"]);

Even using code like this returns NO values:

var adm_s_d = Environment.GetEnvironmentVariable(env + "-adm.s_id");
Log.Debug($"{env}-adm.s_id=" + adm_s_id);

Any help here could be appreciated.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
MB34
  • 4,210
  • 12
  • 59
  • 110
  • _"But, after deployment to our server,"_ - Windows? Linux? Self-hosted? IIS? – Guru Stron May 18 '23 at 20:50
  • 1. Self-hosted IIS, 2. see edit above – MB34 May 18 '23 at 20:51
  • 1. It is either self-hosted or IIS. It can't be both. – Guru Stron May 18 '23 at 20:52
  • Yes, it can, it can be self-hosted on our own servers using IIS – MB34 May 18 '23 at 20:52
  • Usually In discussions which I was part of self-hosted in terms of ASP.NET usually meant that app is hosted as standalone executable or Windows service. – Guru Stron May 18 '23 at 20:55
  • Is your app in-process with `w3wp.exe` or out of process? I suspect the latter might not give your app access to the same environment. – Kit May 18 '23 at 20:58
  • I've got, in web.config: ```processPath="C:\Program Files\dotnet\dotnet.exe"``` and ```hostingModel="InProcess"``` – MB34 May 18 '23 at 20:59
  • We've actually set the environment variables using the user's context but still nothing... – MB34 May 18 '23 at 21:00
  • Have you tried setting environment variables in IIS config ([click1](https://stackoverflow.com/questions/31049152/publish-to-iis-setting-environment-variable), [click2](https://learn.microsoft.com/en-us/iis/configuration/system.applicationhost/applicationpools/add/environmentvariables/))? – Guru Stron May 18 '23 at 21:01
  • Also check out [this answer](https://stackoverflow.com/a/17149834/2501279). Though personally I would expect it to see system variables anyway. Have you tried restarting IIS? – Guru Stron May 18 '23 at 21:02
  • Guru may be on to it. @MB34 you may want to see what `Environment.GetEnvironmentVariables()` returns to see if you have some sort of formatting error as you're building the name. – Kit May 18 '23 at 21:04
  • Kit, adding that code now to see if it lists ANY variables. Guru, the variable value is a secret that we want to hide from users and administrators. Only users that login as the service account can view them and only Devops is able to do that. – MB34 May 18 '23 at 21:07
  • @MB34 So this is not a system variable? Not very strong with Windows options for this case - but maybe create a specific user for the app pool and use this user in IIS for this app (and set variable for this user or include it into specific group). – Guru Stron May 18 '23 at 21:14
  • I currently have the variables set as System variables and will be setting them under the user context soon. What's odd is that using ```Environment.GetEnvironmentVariables()``` returns a bunch of environment variables, it does return SOME of the ones that we set as System variables but not all the ones that we set as System variables, go figure??? – MB34 May 18 '23 at 21:23
  • OK, I have them set as System Environment variables and they are being read using this: ```Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine);``` We're gonna have to set them under the Service Account's User context to see if they can be read them using either ```EnvironmentVariableTarget.User``` or ```EnvironmentVariableTarget.Process``` – MB34 May 18 '23 at 21:50
  • We also presumed that it could be the naming of the variables so we renamed them and set them under the Service Account's user context. In listing the variables for all three EnvironmentVariableTargets, we find that none of the ones we set were listed at all. When we set them as System environment variables, NONE of them were listed either. This is some weird shit, we don't know what else to do here. – MB34 May 19 '23 at 01:57

1 Answers1

0

We found that the loadUserProfile wasn't set to true in the applicationHost.config. We set that and then were able to read the environment variables for the Service Account user.

MB34
  • 4,210
  • 12
  • 59
  • 110