如何在Azure中为会话状态定义连接字符串

Ric*_*olo 5 azure session-state-provider azure-web-sites stackexchange.redis azure-web-app-service

我正在使用RedisSessionStateProvider这样的程序https://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-session-state-caching/

我定义了它的连接字符串web.config,在这个例子中是XXXXXX.

 <system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.5" />
    <globalization culture="es-CO" uiCulture="es" />
    <customErrors mode="Off" />
    <sessionState mode="Custom" customProvider="SessionStateStore">
      <providers>
        <add name="SessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" connectionString="XXXXXX" throwOnError="true" applicationName="NominappSession" />
      </providers>
    </sessionState>
  </system.web>
Run Code Online (Sandbox Code Playgroud)

我不想把连接字符串放在源代码中.那么我如何使用Azure中的设置来定义此连接字符串?

我从github部署到azure,所以它使用Kudu.我没有外部CI服务器.

有什么建议吗?

Ric*_*olo 7

我做的 :)

为此,您需要将连接字符串定义为环境变量,而不是典型的连接字符串.并且在sesion状态中提供使用环境变量名称.

这条路:

  <appSettings>
    <add key="REDIS_CONNECTION_STRING" value="redis,allowAdmin=true" />
  </appSettings>
  <system.web>
    <sessionState mode="Custom" customProvider="SessionStateStore">
      <providers>
        <add name="SessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" connectionString="REDIS_CONNECTION_STRING" applicationName="REDIS_SESSION_APPLICATION_NAME" throwOnError="true" />
      </providers>
    </sessionState>
  </system.web>
Run Code Online (Sandbox Code Playgroud)

使用它,您现在可以在Azure网站的应用程序设置中定义连接字符串


Mar*_*inM 5

如果只想从源代码中提供连接字符串,则可以使用配置中的settingsClassNamesettingsMethodName属性,如下所示:

 <sessionState mode="Custom" customProvider="RedisSessionStateStore">
  <providers>
      <add
        name="RedisSessionStateStore"
        type="Microsoft.Web.Redis.RedisSessionStateProvider"
        settingsClassName="MyApp.SessionStateRedisSettings,MyApp"
        settingsMethodName="ConnectionString" />
  </providers>
Run Code Online (Sandbox Code Playgroud)

在这里,settingsClassName是应用程序中具有完全限定名称空间的类的名称。settingsMethod名称是该类上方法的名称,该名称必须是静态的,采用0个参数并返回一个字符串。例如:

namespace MyApp
{
    public static class SessionStateRedisSettings
    {
        public static string ConnectionString()
        {
            return "ConnectionString goes here";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

从这里:https : //github.com/Azure/aspnet-redis-providers/wiki/Configuration