我想使用CefSharp.Wpf 设置多个浏览器隔离Cookie
时间: 2023-08-08 15:05:54 浏览: 933
您好,针对您的问题,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 冲突。
阅读全文