【问题标题】:Cannot provide password in Connection String for SQLite无法在 SQLite 的连接字符串中提供密码
【发布时间】:2016-10-18 01:46:06
【问题描述】:

我将 SQLite 与实体框架一起使用。

我使用以下代码创建数据库:

class MyContext : DbContext
{   
    // DbSets, OnModelCreating(), etc.

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Data Source=c:\\test.db");
    }
}

//this is in my test method
 using (var db = new MyContext())
{
    db.Database.EnsureCreated();
}

上面的代码有效。数据库被创建。 但我想通过在连接字符串中提供密码来加密数据库:

optionsBuilder.UseSqlite("Data Source=c:\\test.db;Password=mypassword");

在这种情况下,EnsureCreated 被调用后,我得到了异常

System.Data.dll 中出现“System.ArgumentException”类型的未处理异常 附加信息:不支持关键字:“密码”。

使用密码有什么问题?如何加密那个 SQLite 数据库?

【问题讨论】:

标签: c# entity-framework sqlite entity-framework-core


【解决方案1】:

“纯”、开源的 sqlite3.dll 不支持加密。还有其他实现,包括许可和非许可。 对于我的解决方案,我使用了rindeal/SQLite3-Encryption project from GitHub

我的解决方案:

  1. 下载已编译的二进制文件
  2. 将 sqlite3.dll 复制到 BIN 文件夹
    • 在 Visual Studio 中将 DLL 添加到项目中
    • 将复制设置为输出目录 - 始终复制
  3. 在上下文的构造函数中使用这段代码:

    string password;
    var connection = Database.GetDbConnection();
    connection.Open();
    using (var command = connection.CreateCommand())
    {
        command.CommandText = $"PRAGMA key='{password}';";
        command.ExecuteNonQuery();
    }
    

【讨论】:

    【解决方案2】:

    根据这个回答Protect SQLite database used by EntityFramework Core Application

    EF Core 使用 Microsoft.Data.Sqlite,目前不支持 开箱即用的加密。看 https://github.com/aspnet/Microsoft.Data.Sqlite/issues/184。你可以 使用 SQLite 扩展自己添加加密。

    在提供的 GitHub 链接中,还有其他链接到替代的链接

    Encryption in Microsoft.Data.Sqlite

    【讨论】:

    • 这也会抛出异常“Keyword not supported: 'version'.”
    【解决方案3】:

    我找到了一个不涉及在 BIN 文件夹中复制 dll 的解决方案。

    看看it

    【讨论】:

      【解决方案4】:

      我使用了 Tschareck 发布的那个实现:

       private const string Password = "1234";
              protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
              {
                  var conn = new SqliteConnection(@"Data Source=test.db;");
                  conn.Open();
      
                  using (var command = conn.CreateCommand())
                  {
                      command.CommandText = $"PRAGMA key = '{Password}';";
                      command.ExecuteNonQuery();
                  }
                  optionsBuilder.UseSqlite(conn);
              }
      

      并设法将密码输入我的解决方案,解决方案工作正常,但我还有另一个问题 - 我无法使用该密码打开数据库(我正在使用 DB Browser for SQL Lite)(当我单击“确定”密码时)被删除,没有任何反应):

      printScreen DB Browser

      【讨论】:

      • 你有没有偶然发现这个?
      • 数据库浏览器无法打开加密文件。打开它的唯一方法是首先“手动”解锁数据库:将 sqlite3.exe 复制到包含数据库的文件夹中,运行命令行工具运行命令:sqlite3 -key YourKey DBname.db(其中 YourKey 是您用于加密的密钥,并且DBname 是您的数据库的名称)运行命令:.rekey YourKey "" ""
      • 您可以使用其他 SQLite 浏览器 "SQLite2009 Pro Enterprise Manager" sqlite2009pro.azurewebsites.net 如果数据库受密码保护,浏览器会询问您密码
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2016-02-25
      • 1970-01-01
      • 2021-09-17
      相关资源
      最近更新 更多