C# CefSharp WPF 浏览器置cookie字符串方法

            var cookieManager = Cef.GetGlobalCookieManager();
            cookieManager.SetStoragePath(GetAppDir("Cache"), false); /*true为保存cookie*/
            cookieManager.SetSupportedSchemes(new string[] { "http" , "https" });
            string[] arrcookies;
            /*Common.setcookie 为将要cookies字符串*/
            if (!Common.setcookie.IsNullOrEmpty())
            {
                arrcookies = Common.setcookie.Split(';');
                int intEachCookPartsCount = arrcookies.Length;
                for (int i = 0; i < intEachCookPartsCount; i++)
                {
                    if (!arrcookies[i].IsNullOrEmpty())
                    {
                        int IndexofA = arrcookies[i].IndexOf('=');
                        bool status = cookieManager.SetCookie(Common.ToUrl, new Cookie
                        {
                            Name = arrcookies[i].Substring(0, IndexofA),
                            Value = arrcookies[i].Substring(IndexofA + 1),
                        });
                    }
                }
            }
您好,针对您的问题,CefSharp.Wpf 是一个基于 Chromium Embedded Framework(CEF)的 .NET 库,用于在 WPF 应用程序中嵌入浏览器。要实现多个浏览器隔离 Cookie,您可以在代码中创建多个 CefSharp.BrowserSubprocess 对象,并为每个对象设独立的 cookie 存储路径。 具体实现步骤如下: 1. 在代码中创建多个 CefSharp.BrowserSubprocess 对象,例如: ``` var browser1 = new CefSharp.Wpf.ChromiumWebBrowser(); var browser2 = new CefSharp.Wpf.ChromiumWebBrowser(); ``` 2. 设每个浏览器cookie 存储路径,例如: ``` browser1.BrowserSettings = new CefSharp.BrowserSettings { ApplicationCache = CefState.Disabled, WebSecurity = CefState.Enabled, FileAccessFromFileUrls = CefState.Enabled, UniversalAccessFromFileUrls = CefState.Enabled, DefaultEncoding = "UTF-8", JavaScript = CefState.Enabled, JavaScriptAccessClipboard = CefState.Disabled, JavaScriptCloseWindows = CefState.Disabled, JavaScriptDomPaste = CefState.Disabled, LocalStorage = CefState.Enabled, Plugins = CefState.Enabled, RemoteFonts = CefState.Enabled, WebGl = CefState.Enabled, WebAudio = CefState.Enabled, FileAccessFromUrls = CefState.Enabled, ImageLoading = CefState.Enabled, TextAreaResize = CefState.Enabled, WebSecurityEnabled = true, CefCommandLineArgs = new Dictionary<string, string> { { "disable-gpu", "1" }, { "disable-gpu-compositing", "1" }, { "disable-software-rasterizer", "1" } }, PersistSessionCookies = true, FilePath = @"C:\Users\user1\AppData\Local\Temp\cefsharp_cookies1" }; browser2.BrowserSettings = new CefSharp.BrowserSettings { ApplicationCache = CefState.Disabled, WebSecurity = CefState.Enabled, FileAccessFromFileUrls = CefState.Enabled, UniversalAccessFromFileUrls = CefState.Enabled, DefaultEncoding = "UTF-8", JavaScript = CefState.Enabled, JavaScriptAccessClipboard = CefState.Disabled, JavaScriptCloseWindows = CefState.Disabled, JavaScriptDomPaste = CefState.Disabled, LocalStorage = CefState.Enabled, Plugins = CefState.Enabled, RemoteFonts = CefState.Enabled, WebGl = CefState.Enabled, WebAudio = CefState.Enabled, FileAccessFromUrls = CefState.Enabled, ImageLoading = CefState.Enabled, TextAreaResize = CefState.Enabled, WebSecurityEnabled = true, CefCommandLineArgs = new Dictionary<string, string> { { "disable-gpu", "1" }, { "disable-gpu-compositing", "1" }, { "disable-software-rasterizer", "1" } }, PersistSessionCookies = true, FilePath = @"C:\Users\user1\AppData\Local\Temp\cefsharp_cookies2" }; ``` 3. 将浏览器对象添加到 WPF 窗口中,例如: ``` grid.Children.Add(browser1); grid.Children.Add(browser2); ``` 通过以上步骤,您就可以实现多个浏览器隔离 Cookie 的功能了。需要注意的是,每个浏览器cookie 存储路径应该是独立的,以避免 cookie 冲突。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值