简体   繁体   English

在Asp.net MVC中保护连接字符串

[英]Securing Connection String In Asp.net MVC

I have An Asp.net MVC Application With VS.Net2013 in my webconfige file i have connection string Section like this: 我在我的webconfige文件中有一个Asp.net MVC应用程序VS.Net2013我有连接字符串部分,如下所示:

 <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") 我也有限制不使用这种方法(aspnet_regiis.exe -site“EncryptDemo”-app“/”-pe“connectionStrings”)

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. 使用Windows身份验证。 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 这应始终是您首选的方法,除非有一些原因导致您无法使用Windows身份验证并且您被迫使用SQL身份验证

  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. 由于您无法使用aspnet_regiis_exe ,如问题中所述,您可以从代码中加密该部分。 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. 代码取自本网站 ,您可以在那里找到更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM