You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Windows本地用C#获取Edge浏览器指定站点的特定Cookie

获取Edge浏览器中特定站点的指定Cookie(C#实现)

方法一:直接读取Edge的SQLite Cookie数据库

Edge的Cookie存储在SQLite格式的数据库文件中,你已经定位到了存储路径,可按以下步骤获取目标Cookie:

步骤说明

  • 安装SQLite操作包:在项目中安装Microsoft.Data.Sqlite用于读写SQLite数据库。
  • 复制Cookie文件到临时路径:Edge运行时会锁定原Cookie文件,复制一份可避免文件占用问题。
  • 查询并解密Cookie:连接到复制的数据库,筛选目标站点和名称的Cookie,同时处理新版Edge的Cookie加密逻辑。

代码示例

using Microsoft.Data.Sqlite;
using System.IO;
using System.Security.Cryptography;

class Program
{
    static void Main(string[] args)
    {
        string userProfilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
        string edgeUserDataPath = Path.Combine(userProfilePath, "AppData", "Local", "Microsoft", "Edge", "User Data", "Default");
        string cookiesPath = Path.Combine(edgeUserDataPath, "Network", "Cookies");
        
        // 复制到临时文件,规避原文件被Edge锁定的问题
        string tempCookiesPath = Path.Combine(Path.GetTempPath(), "EdgeCookiesTemp.db");
        File.Copy(cookiesPath, tempCookiesPath, overwrite: true);

        // 连接SQLite数据库
        using var connection = new SqliteConnection($"Data Source={tempCookiesPath}");
        connection.Open();

        // 筛选google.com站点下名为XYZ的Cookie
        string query = @"
            SELECT value 
            FROM cookies 
            WHERE host_key LIKE '%google.com' 
              AND name = 'XYZ'";
        
        using var command = new SqliteCommand(query, connection);
        var encryptedValue = command.ExecuteScalar()?.ToString();

        if (!string.IsNullOrEmpty(encryptedValue))
        {
            string decryptedValue = DecryptEdgeCookie(encryptedValue);
            Console.WriteLine($"目标Cookie值:{decryptedValue}");
        }
        else
        {
            Console.WriteLine("未找到指定的Cookie");
        }

        // 清理临时文件
        File.Delete(tempCookiesPath);
    }

    // 解密Edge用DPAPI加密的Cookie值(仅适用于当前Windows用户)
    private static string DecryptEdgeCookie(string encryptedValue)
    {
        byte[] encryptedBytes = Convert.FromBase64String(encryptedValue);
        // 去掉Edge加密Cookie的"v10"前缀字节
        byte[] dataToDecrypt = encryptedBytes.Skip(3).ToArray();
        byte[] decryptedBytes = ProtectedData.Unprotect(
            dataToDecrypt,
            null,
            DataProtectionScope.CurrentUser);
        return System.Text.Encoding.UTF8.GetString(decryptedBytes);
    }
}

方法二:使用WebView2控件(更简单可靠)

WebView2基于Edge内核,可直接获取当前用户的浏览器Cookie,无需处理数据库和加密逻辑,实现更简洁:

步骤说明

  • 安装WebView2包:在项目中安装Microsoft.Web.WebView2。
  • 初始化WebView2:关联当前用户的Edge配置文件。
  • 获取目标Cookie:通过CookieManager直接查询指定站点和名称的Cookie。

代码示例

using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        var webView = new WebView2();
        // 初始化WebView2,使用当前用户的Edge配置
        await webView.EnsureCoreWebView2Async(null);

        // 触发Cookie加载(可直接导航到目标站点或加载空页面)
        await webView.CoreWebView2.NavigateToString("<html></html>");
        await Task.Delay(1000); // 简单等待Cookie加载完成,实际可监听导航完成事件

        // 获取google.com站点下的XYZ Cookie
        var cookieManager = webView.CoreWebView2.CookieManager;
        var targetCookie = await cookieManager.GetCookieAsync("https://www.google.com/", "XYZ");

        if (targetCookie != null)
        {
            Console.WriteLine($"目标Cookie值:{targetCookie.Value}");
        }
        else
        {
            Console.WriteLine("未找到指定的Cookie");
        }
    }
}

注意事项

  • 多用户场景:若Edge存在多个用户配置,Default路径可能不是目标用户的,需切换到对应文件夹(如Profile 1)。
  • WebView2依赖:方法二要求本地已安装Edge浏览器及WebView2运行时。

内容的提问来源于stack exchange,提问作者Bhavish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 23:10:04