static async Task GetLoginUsingSteamClientCookiesAsync() { var value = await ISteamService.Instance.GetLoginUsingSteamClientCookiesAsync(runasInvoker : DI.Platform == Platform.Windows); if (value != default) { var url = value.uri.ToString(); var cookies = value.cookies; var manager = CefRequestContext.GetGlobalContext().GetCookieManager(null); foreach (Cookie item in cookies) { var cookie = new CefNetCookie(item.Name, item.Value); var setCookieResult = await manager.SetCookieAsync(url, cookie); if (item.Name == "steamLoginSecure" && !setCookieResult) { return; } } } }
/// <summary> /// Sets a cookie given a valid URL and explicit user-provided cookie /// attributes. /// </summary> /// <param name="url">The cookie URL.</param> /// <param name="cookie">The cookie.</param> /// <param name="callback"> /// If <paramref name="callback"/> is non-NULL it will be executed /// asnychronously on the CEF UI thread after the cookie has been set. /// </param> /// <returns> /// true if the operation is completed successfully; false if cookies cannot be accessed. /// </returns> /// <exception cref="ArgumentNullException">The <paramref name="url"/> is null or the <paramref name="cookie"/> is null.</exception> /// <exception cref="ArgumentOutOfRangeException">An invalid URL is specified.</exception> public unsafe bool SetCookie(string url, CefNetCookie cookie, CefSetCookieCallback callback) { if (url is null) { throw new ArgumentNullException(nameof(url)); } if (cookie is null) { throw new ArgumentNullException(nameof(cookie)); } if (Uri.TryCreate(url, UriKind.Absolute, out Uri uri) && (Uri.UriSchemeHttp.Equals(uri.Scheme, StringComparison.Ordinal) || Uri.UriSchemeHttps.Equals(uri.Scheme, StringComparison.Ordinal))) { CefCookie aCookie = cookie.ToCefCookie(); try { if (cookie.Domain != null && !cookie.Domain.StartsWith(".")) aCookie.Domain = null; fixed(char *s0 = url) { var cstr0 = new cef_string_t { Str = s0, Length = url.Length }; return(SafeCall(NativeInstance->SetCookie(&cstr0, (cef_cookie_t *)&aCookie, (callback != null) ? callback.GetNativeInstance() : null) != 0)); } } finally { aCookie.Dispose(); } } throw new ArgumentOutOfRangeException(nameof(url)); }
/// <summary> /// Sets a cookie given a valid URL and explicit user-provided cookie attributes. /// This function expects each attribute to be well-formed. It will check for disallowed /// characters (e.g. the ';' character is disallowed within the cookie value attribute) and will return false without setting /// </summary> /// <param name="cookieManager">cookie manager</param> /// <param name="url">The cookie URL. If an empty string is provided, any URL will be matched.</param> /// <param name="cookie">the cookie to be set</param> /// <returns>returns false if the cookie cannot be set (e.g. if illegal charecters such as ';' are used); /// otherwise task that represents the set operation. The value of the TResult parameter contains a bool to indicate success.</returns> public static Task <bool> SetCookieAsync(this CefCookieManager cookieManager, string url, CefNetCookie cookie) { if (cookieManager == null) { throw new NullReferenceException("cookieManager"); } if (cookieManager.IsDisposed) { throw new ObjectDisposedException("cookieManager"); } var callback = new TaskSetCookieCallback(); if (cookieManager.SetCookie(url, cookie, callback)) { return(callback.Task); } //There was a problem setting cookies return(Task.FromResult(false)); }