0

I'm setting up a development template for .Net projects. With that I'm using dot-net-cas-client for authentication backed by ADFS. The issue is, at times our CAS developer changes/migrates the service to new URLs.

Thus, if a plethora of projects were live and an update to the CAS URL has changed. I wouldn't want to manually update CAS URL locations for every live projects' web.config file (I did consider creating a Powershell script that would read through all of the server directories searching for the web.config files and then read through by line and capture the CAS URL strings to replace, but that seems outdated and could be problematic).

I was hoping to use environmental variables at the system or machine level, that way the web.config files could reference one location and obtain the current URL there. Thus only one location would need to be updated when/if a change occurred.

I see that I can add a key to get environment variables' values and use it within the projects code, but I didn't find anything about then using that key or reference directly within the web.config file.

I've attempted to create and reference a key, reference the environment variable directly %environmentVariableName%, but wasn't sure on if it was possible within the web.config.

I attempted to reference the variable %system.variable% directly but didn't obtain the environmental variable value.

dot-net-cas-client settings:

<casClientConfig casServerLoginUrl="https://example.com/cas/login"....

Replaced by something like:

<casClientConfig casServerLoginUrl="%environmentVariableName%"....

OR

<casClientConfig casServerLoginUrl=%environmentVariableName%....

OR

<casClientConfig casServerLoginUrl=Environment.ExpandEnvironmentVariables(ConfigurationManager.AppSetting("environmentVariableName"))....

The above all threw errors (missing quotes) or the request sent me to ...%environmentVariableName%... rather then ...example.com/cas/login...

I understand you can add a KEY which I have also tried:

<add key='SomeSetting' value='%environmentVariableName%'/>

But didn't see anything about referencing that KEY directly in the settings of the web.config file.

dot-net-cas-client "casServerLoginUrl" and other attributes expect a string encapsulated by quotes. Or directed me to my URL, but with %environmentVariableName% instead of the value stored in that variable.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

1 Answers1

0

yeah I'm looking to do something similar also with AWS posting to Environment variables. I haven't found a silver bullet yet, but here's what I've found.

If you want to use the way you're describing above, the variables do not expand automatically (although maybe in AZURE/.NETcore they do???) : How do you put environmental variables in web.config? You have to expand it yourself in all usages - (maybe a wrapper func would be nice) you can also use Environment.GetEnvironmentVariable(myvar)

var someSetting = Environment.ExpandEnvironmentVariables(
                 ConfigurationManager.AppSetting("SomeSetting"))

OR

a powershell way (as you mentioned earlier, it also seems hacky to me, but may be the best solution) https://anthonychu.ca/post/overriding-web-config-settings-environment-variables-containerized-aspnet-apps/

m1m1k
  • 1,375
  • 13
  • 14
  • Hi, Sorry, this will not work. I can create a Key pointing to the string in the Environment Variables, but can't create or use variables within the web.config file. So while I can create describe a variable, I can't use it later within the web.config like: var setting = Environment.ExpandEnvironmentVariables( ConfigurationManager.AppSetting("SomeSetting")); – alexhaberer Jan 30 '20 at 16:51