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

如何在Azure中为会话状态定义连接字符串,azure,azure-web-app-service,stackexchange.redis,session-state-provider,Azure,Azure Web App Service,Stackexchange.redis,Session State Provider,我使用的是redisessionstateprovider过程,如下所示 我在web.config中定义了它的连接字符串,在本例中是XXXXXX <system.web> <compilation debug="true" targetFramework="4.6.1" /> <httpRuntime targetFramework="4.5" /> <globalization culture="es-CO" uiCultur

我使用的是
redisessionstateprovider
过程,如下所示

我在
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>

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

我从github部署到azure,因此它使用Kudu。我没有外部CI服务器


有什么建议吗?

看看斯科特·汉斯曼关于这个主题的博客:

您可以将连接字符串存储在azure门户中的应用程序设置中,然后从应用程序中调用它们。这将防止字符串包含在源代码中。您还需要对存储密钥执行同样的操作

在门户中,转到应用程序的设置,然后选择“应用程序设置”。在该窗格上向下滚动到连接字符串

请查看本页的“连接字符串”部分:
我想IPartitionResolver可以来这里救援

您基本上可以创建一个实现
System.Web.IPartitionResolver
接口的类,并在Web.config中的sessin状态配置中指定它,如下所示

<sessionState mode="Custom"  partitionResolverType="WebAppConnectionStringResolver">
我还没有测试过,但我相信这应该是可行的

阅读更多

    • 我做到了:)

      为此,需要将连接字符串定义为环境变量,而不是典型的连接字符串。在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>
      
      
      

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

      如果您只想从源代码中提供连接字符串,您可以在配置中使用
      设置ClassName
      设置MethodName
      属性,如下所示:

       <sessionState mode="Custom" customProvider="RedisSessionStateStore">
        <providers>
            <add
              name="RedisSessionStateStore"
              type="Microsoft.Web.Redis.RedisSessionStateProvider"
              settingsClassName="MyApp.SessionStateRedisSettings,MyApp"
              settingsMethodName="ConnectionString" />
        </providers>
      

      从此处开始:

      这并没有回答问题,它适用于标准连接字符串和设置,但不适用于会话状态内的连接字符串请注意,Azure应用程序设置将成为环境变量。因此,如果连接字符串配置项支持扩展环境变量(例如,
      connectionString=“%MY\u CONN\u string%”
      ),那么您可能可以实现这一点。或者可能有一个API允许您以编程方式设置conn字符串,也允许您从env变量设置conn字符串。请注意,如果可能的话,请确定,但只是一些想法……这是一个很好的资源,其中包含了您可能想知道的有关像这样配置redis的所有信息。如果您的配置不全部在配置文件中,例如Azure KeyVault,则此解决方案非常好。
       <sessionState mode="Custom" customProvider="RedisSessionStateStore">
        <providers>
            <add
              name="RedisSessionStateStore"
              type="Microsoft.Web.Redis.RedisSessionStateProvider"
              settingsClassName="MyApp.SessionStateRedisSettings,MyApp"
              settingsMethodName="ConnectionString" />
        </providers>
      
      namespace MyApp
      {
          public static class SessionStateRedisSettings
          {
              public static string ConnectionString()
              {
                  return "ConnectionString goes here";
              }
          }
      }