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

How to define connection string for session state in Azure

我正在使用 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>

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

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

有什么建议吗?

查看 Scott Hanselman 关于该主题的博客: http://www.hanselman.com/blog/HowToKeepYourASPNETDatabaseConnectionStringsSecureWhenDeployingToAzureFromSource.aspx

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

在门户中转到您的应用设置,然后select "Application Settings"。在该窗格上向下滚动到连接字符串。

同时查看此页面的连接字符串部分: https://azure.microsoft.com/en-us/documentation/articles/web-sites-configure/

我觉得IPartitionResolver可以来拯救这里...

你基本上可以创建一个实现 System.Web.IPartitionResolver 接口的 class 并在 web.config 的 sessin 状态配置中将其指定为 this

<sessionState mode="Custom"  partitionResolverType="WebAppConnectionStringResolver">

然后在class中你可以覆盖连接字符串

public class WebAppConnectionStringResolver : System.Web.IPartitionResolver
{
   public void Initialize()
   {

   }

   public string ResolvePartition(object key)
   {
     return System.Configuration.ConfigurationManager.ConnectionStrings["your_Conn_string_name_in_portal"]
   }
}

我还没有对此进行测试,但我相信这应该有效

阅读更多内容

我做到了:)

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

这样:

  <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 网站的应用程序设置中定义连接字符串

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

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

在这里,settingsClassName 是您应用中 class 的名称,具有完全限定的命名空间。 settingsMethod name是这个class上的方法名,必须是静态的,带0个参数,return一个字符串。例如:

namespace MyApp
{
    public static class SessionStateRedisSettings
    {
        public static string ConnectionString()
        {
            return "ConnectionString goes here";
        }
    }
}

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