【问题标题】:How to encrypt and decrypt connection string in ASP.NET MVC 4?如何在 ASP.NET MVC 4 中加密和解密连接字符串?
【发布时间】:2014-09-15 11:27:50
【问题描述】:

我想在 Entity Framework 5 中使用它时对连接字符串进行加密和解密。

我做了什么,我的连接字符串是

<add name="HContext" connectionString="Data Source=(localdb)\Projects;Initial Catalog=MyDBName;Integrated Security=True;" providerName="System.Data.SqlClient" />

我已经在下面加密了这个连接

string encryptedString = KimberlyClark.EuroCRM.Encryption.Encrypt(encryptedString, "mykey");

之后,我将加密字符串复制到我的 web.config 文件中并像下面这样使用

public class HContext : DbContext
{
    public HContext()
        : base("HContext") 
    {
        string encryptedString = System.Configuration.ConfigurationManager.ConnectionStrings["HContext"].ConnectionString;
        string connectString = KimberlyClark.EuroCRM.Encryption.Decrypt(encryptedString, "mykey");
        Database.Connection.ConnectionString = connectString;
    }

}

但我在构造函数中遇到错误

初始化字符串的格式不符合从索引 0 开始的规范

谢谢

【问题讨论】:

    标签: asp.net-mvc-4 c#-4.0 encryption entity-framework-5


    【解决方案1】:

    我可以在您的代码中看到,您没有加密连接字符串。 检查这一行:

    string encryptedString = KimberlyClark.EuroCRM.Encryption.Encrypt(encryptedString, "mykey");
    

    你应该传递一个参数,你的连接字符串,而不是 encryptedString 变量。

    【讨论】:

    • Hi 已经加密并将该字符串复制到 web.config
    • 你能检查解密的连接字符串吗?解密后结果如何?
    • 解密后我得到这个字符串“Data Source=(localdb)\Projects;Initial Catalog=MyDBName;Integrated Security=True;” providerName="System.Data.SqlClient"
    • 好的。所以不存在加密错误。可能您的连接字符串无效。请查看此地址:connectionstrings.com
    • 我通过将连接字符串名称从 HContext 更改为 HKContext 来实现它。
    猜你喜欢
    • 2020-11-21
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 2015-02-04
    相关资源
    最近更新 更多