Securing Connection String In Asp.net MVC

不羁的心 提交于 2021-02-07 04:32:25

问题


I have An Asp.net MVC Application With VS.Net2013 in my webconfige file i have connection string Section like this:

 <connectionStrings>
    <add name="ConnectStrNL" connectionString="server=192.168.0.71\ins1;database=FNHProvider;MultipleActiveResultSets=true;persist  security info=True;User ID=general;Password=123;" />
    <add name="connectionStringGeneral" connectionString="server=192.168.0.254;database=NFS;MultipleActiveResultSets=true;persist security info=True;User ID=General;Password=*******;" />
</connectionStrings>

i wanted to Hide User And Pass Of Databases From Every one. and also i have limitation not to use this method (aspnet_regiis.exe -site "EncryptDemo" -app "/" -pe "connectionStrings")


回答1:


There are 2 basic things that you can do if you don't want your password to be in the configuration file:

  1. Use Windows authentication. This should always be you preferred approach unless there are some reasons why you cannot use Windows authentication and you are forced to use SQL authentication

  2. Encrypt the connection string. Since you cannot use aspnet_regiis_exe, as you mentioned in the question, you can encrypt the section from the code. The below code should be run once at the start of the application:

    using System.Web.Configuration;
    using System.Web.Security;
    using System.Configuration;
    
    public void EncryptConnString()
    {
        Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        ConfigurationSection section = config.GetSection("connectionStrings");
        if (!section.SectionInformation.IsProtected)
        {
            section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
            config.Save();
        }
    }
    

The code was taken from this site, you can find more information there.



来源:https://stackoverflow.com/questions/26082629/securing-connection-string-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!