特定のサイトの Cookie を読み取る必要があり、おそらく役立つコードをインターネット上で見つけました。このコードは SQLite のいくつかのメソッド固有を使用しており、これを作成するには、いくつかの SQLite dll への参照を追加する必要がありますが、問題は、SQlite.Interop.dll を追加しようとすると、次のようなエラーが生成されることです。
「C:\Program Files\System.Data.SQLite\2012\bin\SQLite.Interop.dll」への参照を追加できませんでした。ファイルにアクセスできること、および有効なアセンブリまたは COM コンポーネントであることを確認してください。
だから、誰かがこの問題を解決するのを手伝ってくれる. 私はすでにインターネット上のいくつかのサイトでそれに関連するものを見ましたが、今まで成功していませんでした.
これは私が見つけたコードで、上で述べたように、彼は SQLite dll ファイルの参照を必要としています。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static public IEnumerable<Tuple<string, string>> ReadCookies(string hostName)
{
if (hostName == null) throw new ArgumentNullException(hostName);
var dbPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Google\Chrome\User Data\Default\Cookies";
if (!System.IO.File.Exists(dbPath)) throw new System.IO.FileNotFoundException("Cant find cookie store", dbPath);
var connectionString = "Data Source=" + dbPath + ";pooling=false";
using (var conn = new System.Data.SQLite.SQLiteConnection(connectionString))
using (var cmd = conn.CreateCommand())
{
var prm = cmd.CreateParameter();
prm.ParameterName = hostName;
prm.Value = hostName;
cmd.Parameters.Add(prm);
cmd.CommandText = "SELECT name,encrypted_value FROM cookies WHERE host_key = " + hostName;
conn.Open();
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var encryptedData = (byte[])reader[1];
var decodedData = System.Security.Cryptography.ProtectedData.Unprotect(encryptedData, null, System.Security.Cryptography.DataProtectionScope.CurrentUser);
var plainText = Encoding.ASCII.GetString(decodedData);
yield return Tuple.Create(reader.GetString(0), plainText);
}
}
conn.Close();
}
}
static void Main(string[] args)
{
var list = ReadCookies("facebook.com");
foreach (var item in list)
Console.WriteLine("{0} | {1}", item.Item1, item.Item2);
Console.WriteLine();
Console.ReadLine();
}
}
}