2011-03-06 85 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"); 
相关问题