2011-03-06 64 views
0

我需要在我的asp.net應用程序中保持對不同網頁的訪問設置。這些設置是這些頁面的登錄名和密碼。是否足夠安全,以保持它們在web.config中的部分?如何在ASP.NET應用程序中存儲帳戶訪問信息

+0

你能詳細點嗎?你的意思是允許頁面的角色?爲什麼要將登錄名和密碼存儲到web.config中的單個頁面?您可以使用Forms Authentication對用戶進行身份驗證,然後通過web.config設置對用戶進行授權。 – 2011-03-06 23:48:39

+0

考慮爲每個頁面使用角色而不是多個用戶/密碼設置。如果你使用ASP.NET MVC - 區域的概念會做。在典型的ASP.NET世界中,您可以使用文件夾來分隔頁面。 – sajoshi 2011-03-07 03:19:46

+0

我的意思是完全訪問另一項服務。讓我們說我有一些房子,單位提供,我需要通過FTP在專用服務上輸出爲壓縮文件。我需要爲所有這些服務保持登錄和密碼設置。 – bartebly 2011-03-09 22:57:34

回答

0

是的,你可以保存在你的web.config中的信息和密碼,你可以通過加密來保護這些部分。我不知道這是否是最適合這樣做的地方,但考慮到您的描述,我會認爲這是您案例的最佳解決方案。

這裏是實現加密一條有效的途徑:What is the proper method for encrypting ASP.NET connetionStrings?

private void ProtectSection(string sectionName, 
            string provider) 
{ 
    Configuration config = 
     WebConfigurationManager. 
      OpenWebConfiguration(Request.ApplicationPath); 

    ConfigurationSection section = 
       config.GetSection(sectionName); 

    if (section != null && 
       !section.SectionInformation.IsProtected) 
    { 
     section.SectionInformation.ProtectSection(provider); 
     config.Save(); 
    } 
} 

private void UnProtectSection(string sectionName) 
{ 
    Configuration config = 
     WebConfigurationManager. 
      OpenWebConfiguration(Request.ApplicationPath); 

    ConfigurationSection section = 
       config.GetSection(sectionName); 

    if (section != null && 
      section.SectionInformation.IsProtected) 
    { 
     section.SectionInformation.UnprotectSection(); 
     config.Save(); 
    } 
} 

因此,爲了保護設置,你只需要調用與您希望保護的部分,您所選擇的保護提供了ProtectSection方法(通常是DataProtectionConfigurationProviderRSAProtectedConfigurationProvider):

ProtectSection("appSettings", "DataProtectionConfigurationProvider"); 

要解除調用UnProtectSection法截面的截面要取消保護:

UnProtectSection("appSettings"); 
相關問題