首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >管理/删除工作WebView2的cookie

管理/删除工作WebView2的cookie
EN

Stack Overflow用户
提问于 2020-10-10 12:59:25
回答 1查看 5.1K关注 0票数 1

我正在运行边缘WebView2的一些登录页面加载在那里。我想自动登录。

要做到这一点,我需要删除当前的cookie。我不能通过js做到这一点,因为它们是httponly。对于边缘测试版浏览器,我为删除cookie编写了简单的Chrome扩展,但是我不能在WebView2中运行一个扩展(或者可以吗?)

此外,我知道WebView2 cookie文件位于何处,但在运行Webview时无法更改它。

在WebView中这样做的唯一方法是打开DevTools,我在应用程序选项卡中删除了它们。

关于如何删除那个饼干有什么想法吗?

我希望至少有一个WebView2页面加载c++中带有自定义头的示例(在这里我可以指定cookies)。

EN

回答 1

Stack Overflow用户

发布于 2022-09-21 14:37:30

您可以尝试在下面的代码中创建、更新和从WebView获取cookie

代码语言:javascript
复制
#include <string>
#include <tchar.h>
#include <wrl.h>
#include <wil/com.h>
#include "WebView2.h"
using namespace Microsoft::WRL;
std::wstring uri = L"https://developer.microsoft.com/en-us/microsoft-edge/webview2/";
static wil::com_ptr<ICoreWebView2Controller> webviewController;
static wil::com_ptr<ICoreWebView2> webview;
static wil::com_ptr<ICoreWebView2_2> webview2;
static wil::com_ptr<ICoreWebView2CookieManager> cookieManager;
static wil::com_ptr<ICoreWebView2Cookie> cookie;

void WebView2Initialise(HWND & hWnd)/*HWND of MainWindow, call this after Window initialisation */ {
//Create a single WebView within the parent window
//Locate the browser and set up the environment for WebView
CreateCoreWebView2EnvironmentWithOptions(nullptr, nullptr, nullptr,
Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
        [hWnd](HRESULT result, ICoreWebView2Environment* env) -> HRESULT{

            // Create a CoreWebView2Controller and get the associated CoreWebView2 whose parent is the main window hWnd
            env->CreateCoreWebView2Controller(hWnd, Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
                [hWnd](HRESULT result, ICoreWebView2Controller* controller) -> HRESULT {
                    if (controller != nullptr) {
                        webviewController = controller;
                        if (webviewController)
                        {
                            webviewController->get_CoreWebView2(&webview);
                            if (webview)
                            {
                                webview->QueryInterface(IID_PPV_ARGS(&webview2));
                                webview2->get_CookieManager(&cookieManager);
                            }
                        }
                    }

                    // Add a few settings for the webview
                    // The demo step is redundant since the values are the default settings
                    wil::com_ptr<ICoreWebView2Settings> settings;
                    
                    webview->get_Settings(&settings);
                    if (settings)
                    {
                        settings->put_IsScriptEnabled(TRUE);
                        settings->put_AreDefaultScriptDialogsEnabled(TRUE);
                        settings->put_IsWebMessageEnabled(TRUE);
                    }

                    // Resize WebView to fit the bounds of the parent window
                    RECT bounds;
                    GetClientRect(hWnd, &bounds);
                    webviewController->put_Bounds(bounds);
                    std::wstring uri = L"https://developer.microsoft.com/en-us/microsoft-edge/webview2/";

                    // Schedule an async task to navigate to Bing
                    webview->Navigate(uri.c_str());

                    if (cookieManager)
                    {
                        //Create Cookies
                        cookieManager->CreateCookie(L"CookieName", L"CookieValue", uri.c_str(), L"/", &cookie);
                        cookieManager->AddOrUpdateCookie(cookie.get());

                        cookieManager->GetCookies(
                            uri.c_str(),
                            Callback<ICoreWebView2GetCookiesCompletedHandler>(
                                [](HRESULT error_code, ICoreWebView2CookieList* list) -> HRESULT {
                                    std::wstring result;
                                    UINT cookie_list_size;
                                    list->get_Count(&cookie_list_size);

                                    if (cookie_list_size == 0)
                                    {
                                        result += L"No cookies found.";
                                    }
                                    else
                                    {
                                        result += std::to_wstring(cookie_list_size) + L" cookie(s) found";
                                        if (!uri.empty())
                                        {
                                            result += L" on " + uri;
                                        }
                                        result += L"\n\n[";
                                        for (UINT i = 0; i < cookie_list_size; ++i)
                                        {
                                            wil::com_ptr<ICoreWebView2Cookie> cookie;
                                            list->GetValueAtIndex(i, &cookie);

                                            if (cookie.get())
                                            {
                                                LPWSTR value;
                                                cookie->get_Value(&value);
                                                result += value;
                                                if (i != cookie_list_size - 1)
                                                {
                                                    result += L",\n";
                                                }
                                            }
                                        }
                                        result += L"]";
                                    }

                                ::MessageBox(NULL, result.c_str(), L"Cookies", MB_OK);
                                    
                                    return S_OK;
                                })
                            .Get());
                    }}   
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64293698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档