Cannot provide password in Connection String for SQLite

亡梦爱人 提交于 2019-12-01 04:25:47

问题


I use SQLite with Entity Framework.

I create the DB with following code:

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();
}

The above code works. The DB gets created. But I want to encrypt the DB by providing password in connection string:

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

In this case, after EnsureCreated is called, I get exception

An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: Keyword not supported: 'password'.

What is wrong with using password? How can I encrypt that SQLite database?


回答1:


According to this answer Protect SQLite database used by EntityFramework Core Application

EF Core uses Microsoft.Data.Sqlite, which does not currently support encryption out-of-box. See https://github.com/aspnet/Microsoft.Data.Sqlite/issues/184. You could add encryption yourself using SQLite extensions.

At the GitHub link provided there were other links to an alternative at

Encryption in Microsoft.Data.Sqlite




回答2:


The "pure", open source sqlite3.dll does not support encryption. There are other implementations, including licenced and unlicensed. For my solution I have used rindeal/SQLite3-Encryption project from GitHub

My solution:

  1. Download compiled binaries
  2. Copy sqlite3.dll to BIN folder
    • add the DLL to project in Visual Studio
    • set Copy to Output Directory - Copy always
  3. Use this code in context's constructor:

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



回答3:


I found a solution that does not involve copying the dll within the BIN folder.

Take a look at it.




回答4:


I've used that implementation that Tschareck posted:

 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);
        }

and managed to put the password to my solution, and solution works fine, but I have another problem - I cannot open database (I'm using DB Browser for SQL Lite) using that password (when I click "Ok" Password gets deleted and nothing happens):

printScreen DB Browser



来源:https://stackoverflow.com/questions/37860933/cannot-provide-password-in-connection-string-for-sqlite

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