Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Environment Variables in Connection Strings #128

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Shared/ProviderConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,18 @@ private static string GetConnectionStringFromConfig(NameValueCollection config,
string appSettingsValue = GetFromAppSetting(literalValue);
if (!string.IsNullOrEmpty(appSettingsValue))
{
appSettingsValue = Environment.ExpandEnvironmentVariables(appSettingsValue);
return appSettingsValue;
}

if (!string.IsNullOrWhiteSpace(literalValue)
&& ConfigurationManager.ConnectionStrings[literalValue] != null
&& !string.IsNullOrWhiteSpace(ConfigurationManager.ConnectionStrings[literalValue].ConnectionString))
{
return ConfigurationManager.ConnectionStrings[literalValue].ConnectionString;
var connectionString = ConfigurationManager.ConnectionStrings[literalValue].ConnectionString;
connectionString = Environment.ExpandEnvironmentVariables(connectionString);

return connectionString;
}
return literalValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,22 @@ public void GetConnectionString_Valid()
config.Add(settingsMethodName, "GetSettings");
Assert.Equal("localhost:6380", ProviderConfiguration.GetConnectionString(config));
}

[Fact]
public void UseConnectionStringByNameWithEnvironmentVariables()
{
const string environmentVariableName = "__ASPNET_REDIS_CONNECTION_STRING__";
const string expectedConnectionString = "localhost:6380,localhost:6381";

Environment.SetEnvironmentVariable(environmentVariableName, expectedConnectionString);

NameValueCollection config = new NameValueCollection();
config.Add("connectionString", "RedisSession2");
Assert.Equal(expectedConnectionString,
ProviderConfiguration.ProviderConfigurationForSessionState(config).ConnectionString);

Environment.SetEnvironmentVariable(environmentVariableName, null);
}
}

public class Logger
Expand Down
1 change: 1 addition & 0 deletions test/RedisSessionStateProviderUnitTest/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<configuration>
<connectionStrings>
<add name="RedisSession" connectionString="localhost:6380"/>
<add name="RedisSession2" connectionString="%__ASPNET_REDIS_CONNECTION_STRING__%" />
</connectionStrings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
Expand Down