2011-10-20 5 views
1

web.configの接続文字列の暗号化機能に問題があります。web.configの暗号化された接続文字列でエラーが発生する

暗号化は完全に機能します。しかし、暗号化が有効になるとすぐに私はセッション変数の内容を失う(セッション変数のNull例外)。

web.configで接続文字列の暗号化を無効にすると、すべて正常に戻ります。

#region Constructeur 

static QueryManager() 
{ 
    Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); 
    ConnectionStringsSection section = config.GetSection("connectionStrings") as 
            ConnectionStringsSection; 

    if (section.SectionInformation.IsProtected) 
    { 
    section.SectionInformation.UnprotectSection(); 
    config.Save(ConfigurationSaveMode.Minimal); 
    } 

    if ((myConnectionString = 
     ConfigurationManager.ConnectionStrings["DBConnect"].ConnectionString) == null) 
    { 
    throw new ConfigurationErrorsException("Database server not configured"); 
    } 

    section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); 
    config.Save(ConfigurationSaveMode.Minimal);    
} 

#endregion 

あなたの助けのおかげ百万:

は、ここで接続文字列の暗号化のための私のコードです!

+0

私のコードは、このWebサイトに基づいています。http://msdn.microsoft.com/en-us/library/89211k9b%28v=vs.80%29.aspx –

答えて

1

このエラーは設計ミスによるものです。ここで

がソリューションです:

  • まず、暗号化は、暗号化/復号化にデータベースへの要求が行われるたびに保存することを回避するために、アプリケーションの外部から行われる必要があります。
  • その後

static QueryManager() 
{ 

    Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); 
    ConnectionStringsSection section = config.GetSection("connectionStrings") as 
            ConnectionStringsSection; 

    if (section.SectionInformation.IsProtected) 
    { 
    section.SectionInformation.UnprotectSection(); 
    }    

    myConnectionString = section.ConnectionStrings["DBConnect"].ConnectionString; 

    if (unikSignConnectionString == null) 
    { 
    throw new ConfigurationErrorsException("Database server not configured"); 
    } 
} 

そのように、接続文字列は、メモリ内に復号化して使用するすべての問題を作成せずに、それは、多くの無駄な読み込みを避け、web.configファイルに書き込みされています。

関連する問題