如何为Chromium嵌入式浏览器的不同实例设置不同的Cookie [英] How to set different cookies for different instances of Chromium embedded browser
问题描述
我正在开发一个需要多个嵌入式实例的应用程序,并且每个实例登录到具有不同身份验证详情的同一个外部网站。
I'm working on an application that requires multiple embedded instances and each of this instance logins to the same external site with different authentication details.
CEF(Chromium embedded framework)在Delphi中,我有一个文件夹,我存储cookie为它的不同的实例我的浏览器,所以我有这个代码在第一个单位
I'm using CEF (Chromium embedded framework ) in Delphi , I have a folder that i stored the cookies on it for different instance of my browsers , So i have this code in the first unit
procedure TForm2.Button1Click(Sender: TObject);
begin
form33 := Tform3.Create(nil);
form33.Show;
end;
当form33创建
procedure TForm3.FormCreate(Sender: TObject);
var
CookieManager: ICefCookieManager;
folder: string;
begin
Randomize;
Chromium1.SetBrowserID(Random(1244));
folder := Randomtext(5);
if DirectoryExists(folder) = False then
MkDir(folder);
CookieManager := TCefCookieManagerRef.Global;
path := ExtractFilePath(Application.ExeName) + folder;
CookieManager.SetStoragePath(path, true);
end;
问题是当我打开两个或更多的form33实例时,对每个表单中的每个浏览器33一个特定的cookie ...
The problem is that when i open two or more of instances of the form33 , i can't have for each browser in each form33 a specific cookie ...
推荐答案
您应该为每个实例创建一个新的管理器 TCefCookieManagerRef.New(Path)
并将返回的引用存储在窗体的某个私有字段中。然后,您需要将存储的引用返回到 OnGetCookieManager
事件方法的 Result
参数:
You should create a new manager for each instance by TCefCookieManagerRef.New(Path)
and store the returned reference into some private field of your form. Then you need to return the stored reference to the Result
parameter of the OnGetCookieManager
event method:
type
TForm1 = class(TForm)
Chromium1: TChromium;
procedure FormCreate(Sender: TObject);
procedure Chromium1GetCookieManager(Sender: TObject;
out Result: ICefCookieManager);
private
FCookieManager: ICefCookieManager2;
end;
implementation
procedure TForm1.FormCreate(Sender: TObject);
begin
FCookieManager := TCefCookieManagerRef.New('C:\UniquePathToTheCookieStorage');
end;
procedure TForm1.Chromium1GetCookieManager(Sender: TObject;
out Result: ICefCookieManager);
begin
Result := FCookieManager;
end;
这篇关于如何为Chromium嵌入式浏览器的不同实例设置不同的Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!