【混合开发】CefSharp+Vue 解决Cookie问题

发布于:2025-02-10 ⋅ 阅读:(53) ⋅ 点赞:(0)

问题表现

使用Element-admin架构搭建Vue前端项目,在与CefSharp搭配时,出现无法使用cookie的问题。

  1. 无法将token存入cookie
  2. cookie无法被读取

如下图,Cookies下显示file://。

在这里插入图片描述

正常的Cookies显示,Cookies显示为http://域名,如下图:

在这里插入图片描述

解决问题

经过确认,已肯定:

  • 以Url加载的服务方式的前端可以通过C#动态添加或删除Cookie。
  • 以本地化方式加载的前端无法通过C#动态添加或删除Cookie。

如果你是Url加载服务方式的前端,如下解决

//初始化浏览器对象
string webUrl = "http://127.0.0.1:8080";
chromeBrowser = new ChromiumWebBrowser(webUrl);
chromeBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
this.Controls.Add(chromeBrowser);

//加入cookie
CefSharp.Cookie cookie = new CefSharp.Cookie();
cookie.Name = "cookie_name";
cookie.Value = "cookie_value";
cookie.Domain = "";//为空表示使用域名
cookie.Path = "";//为空表示使用/
cookie.Expires = DateTime.Now.AddDays(1);

ICookieManager cookieManager = Cef.GetGlobalCookieManager();
//IP或域名必须与实际前端服务保持一致
cookieManager.SetCookie(webUrl, cookie);

如果你是本地化方式加载的前端,如下解决

创建一个Cookie类:

   public class CookieUtil
    {
        public static CookieUtil Instance = new CookieUtil();

        private Dictionary<string, string> dicCookie = new Dictionary<string, string>();

        public string GetCookie(string name)
        {
            if (dicCookie.ContainsKey(name))
            {
                return dicCookie[name];
            }
            else
            {
                return "";
            }
        }

        public void AddCookie(string name,string value)
        {
            if (dicCookie.ContainsKey(name))
            {
                 dicCookie[name]=value;
            }
            else
            {
                dicCookie.Add(name,value);
            }
        }

        public void RemoveCookie(string name)
        {
            dicCookie.Remove(name);
        }

        public void ClearAll()
        {
            dicCookie = new Dictionary<string, string>();
        }
    }

创建AppService类:

    public class AppService
    {
        public string login(string username, string password)
        {
            string tokenValue = "admin-token";
            return JsonUtil.Instance.Success(new { token = tokenValue });
        }
        public string getCookie(string name)
        {
            return CookieUtil.Instance.GetCookie(name);
        }

        public void setCookie(string name, string value)
        {
            CookieUtil.Instance.AddCookie(name, value);
        }

        public void removeCookie(string name, string value)
        {
            CookieUtil.Instance.AddCookie(name, value);
        }

        public void clearAll()
        {
            CookieUtil.Instance.ClearAll();
        }
    }

初始化浏览器类:

//初始化浏览器对象
string webUrl = "http://127.0.0.1:8080";
chromeBrowser = new ChromiumWebBrowser(webUrl);
chromeBrowser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
chromeBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
//声明使用C#端服务对象
chromeBrowser.JavascriptObjectRepository.Register("cefAppService", new AppService(), isAsync: false, options: BindingOptions.DefaultBinder);
this.Controls.Add(chromeBrowser);

网站公告

今日签到

点亮在社区的每一天
去签到