7

我正在使用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 服务器。

请问有什么建议吗?

4

4 回答 4

8

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

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

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

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

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

于 2018-02-01T15:36:39.817 回答
8

我做的 :)

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

这边走:

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

于 2016-02-26T00:59:11.643 回答
0

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

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

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

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

于 2016-02-20T22:51:22.433 回答
0

我认为IPartitionResolver可以来这里救援......

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

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

然后在课堂上你可以覆盖连接字符串

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"]
   }
}

我还没有测试过,但我相信这应该有效

阅读更多

于 2016-02-22T15:10:49.770 回答