Retrieve data from browser local storage using c#

后端 未结 2 973
温柔的废话
温柔的废话 2021-02-04 10:56

Is it possible to retrieve data from chrome/firefox local storage using C#?

2条回答
  •  猫巷女王i
    2021-02-04 11:56

    Disclaimer: I have tested this on my Windows 7 x64 running Google Chrome 13.0.782.220 at the moment. The information provided here is a result of my own research and is not any official way or API to retrieve this information. Use at your own risk. Also the technique presented here might break with any future release if Chrome changes the way to store this information.


    So, Google Chrome uses SQLite to persist local storage data. You could use the System.Data.SQLite managed driver to read it from your .NET application. If you are running on Windows 7 (don't know for others as that's the one I have and can test), you will have the following folder:

    c:\Users\SOMEUSERNAME\AppData\Local\Google\Chrome\User Data\Default\Local Storage\
    

    This folder will contain multiple files with the .localstorage extension. Each file is for different site. For example for StackOverflow I have http_stackoverflow.com_0.localstorage but of course this naming is totally arbitrary and you cannot rely upon it. Each file represents a SQLite database.

    I have noticed that this database contains a table called ItemTable with 2 string columns called key and value.

    So to read the values it's a simple matter of sending a SQL query:

    class Program
    {
        static void Main()
        {
            using (var conn = new SQLiteConnection("Data Source=http_stackoverflow.com_0.localstorage;Version=3;"))
            using (var cmd = conn.CreateCommand())
            {
                conn.Open();
                cmd.CommandText = "SELECT key, value FROM ItemTable";
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Console.WriteLine(
                            "key: {0}, value: {1}", 
                            reader.GetString(reader.GetOrdinal("key")),
                            reader.GetString(reader.GetOrdinal("value"))
                        );
                    }
                }
            }
        }
    }
    

提交回复
热议问题