-1

I tried to create a saved password reader in Chrome but when I open the connection in Sqllite it fails p.s. var conn is not null. The only error I can notice is in ServerVersion = <'conn.ServerVversion' threw an exception of type 'System.NullReferenceException'>

public IEnumerable<CredentialModel> ReadPasswords()
{
    var result = new List<CredentialModel>();

    var appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    var p = Path.GetFullPath(appdata + LOGIN_DATA_PATH);

    if (File.Exists(p))
    {
        using (var conn = new SqliteConnection($"Data Source={p};"))
        {
            conn.Open();
            using (var cmd = conn.CreateCommand())
            {
                cmd.CommandText = "SELECT action_url, username_value, password_value FROM logins";
                using (var reader = cmd.ExecuteReader())
                {

                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            var pass = Encoding.UTF8.GetString(ProtectedData.Unprotect(GetBytes(reader, 2), null, DataProtectionScope.CurrentUser));

                            result.Add(new CredentialModel()
                            {
                                Url = reader.GetString(0),
                                Username = reader.GetString(1),
                                Password = pass
                            });
                        }
                    }
                }
            }
            conn.Close();
        }

    }
    else
    {
        throw new FileNotFoundException("Canno find chrome logins file");
    }
    return result;
}
vernou
  • 6,818
  • 5
  • 30
  • 58
  • Is `conn.ServerVversion` in the error message a typo, or is that the actual message? If it's not a typo, there's an extra `v`. I don't see that code in what you posted though. – Diado Sep 03 '20 at 10:05
  • Are you using latest SQL Driver? The Net driver is probably different from the chrome driver. You may have an old SQLLite driver. See : https://www.connectionstrings.com/sqlite/ – jdweng Sep 03 '20 at 10:07
  • @Diado if I check conn in ServerVersion it says ServerVersion 'conn.ServerVersion' threw an exception of type 'System.NullReferenceException' string {System.NullReferenceException} – Francesco Buscicchio Sep 03 '20 at 10:10
  • The extra `v` is presumably a typo then. At what stage in the code are you checking `conn.ServerVersion`? – Diado Sep 03 '20 at 10:11
  • 1
    @FrancescoBuscicchio that doesn't mean anything. Just because a property can't be read in the Watch Window doesn't mean the class is broken. Do you get an exception in the code? What is the *exact* text returned by `Exception.ToString()`? Do you get an exception because the connection string is invalid perhaps? – Panagiotis Kanavos Sep 03 '20 at 10:15
  • @PanagiotisKanavos {"Reference to an object not set to an object instance."} – Francesco Buscicchio Sep 03 '20 at 10:25
  • 1
    @FrancescoBuscicchio that's not the exception's text, that's a JSON string generated by just the `Message` part. Post the **full** exception text. That includes the exact line where the error occurred and the chain of calls that led to it. – Panagiotis Kanavos Sep 03 '20 at 10:27
  • @PanagiotisKanavos ex.Source = 'SQLitePCLRaw.core' ex.StackTrace = ' at SQLitePCL.raw.sqlite3_open_v2(String filename, sqlite3& db, Int32 flags, String vfs) at Microsoft.Data.Sqlite.SqliteConnection.Open() at Password_Tracker.ChromePassReader.ReadPasswords() in C:\Users\francesco.buscicchio\source\repos\Password Tracker\ChromePassReader.cs:line 31' – Francesco Buscicchio Sep 03 '20 at 10:40
  • 1
    Please post the *full exception text in the query itself*, not just parts of it. Are you *sure* the path is correct? Have you checked the value of `p`? `Environment.GetFolderPath` doesn't return a trailing `\\`. Use `Path.Combine` to create paths – Panagiotis Kanavos Sep 03 '20 at 10:57

1 Answers1

2

Your project is referencing Microsoft.Data.Sqlite.Core, which also requires work to setup which native SQLite library to use. It seems that the native SQLite driver is not setup correctly in your case.

For an easier experience, reference Microsoft.Data.Sqlite which pre-configures the SQLite to use.

Source

George Chondrompilas
  • 3,167
  • 27
  • 35