从WpfEmbeddedBrowser中删除Cookie-WPF

时间:2020-07-27 17:32:21

标签: c# wpf cookies browser webbrowser-control

我找到了一个在线类,该类用于从WPF登录到Identity Server API。我需要以编程方式删除其Cookie。我在网上其他地方不断找到的代码似乎与清除WebBrowser控件(特别是WPF窗口)中的cookie有关。我没有任何一个可以工作,所以也许不一样。

这是WpfEmbeddedBrowser:

public class WpfEmbeddedBrowser : IBrowser
    {
        private BrowserOptions _options = null;

        public WpfEmbeddedBrowser()
        {

        }

        public async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = default)
        {
            _options = options;

            var window = new Window()
            {
                Width = 450,
                Height = 750,
                Title = "SiteMonitor Desktop Application Login"
            };

            var webBrowser = new WebBrowser();

            var signal = new SemaphoreSlim(0, 1);
            window.Show();
            var result = new BrowserResult()
            {
                ResultType = BrowserResultType.UserCancel
            };

            webBrowser.Navigating += (s, e) =>
            {
                if (BrowserIsNavigatingToRedirectUri(e.Uri))
                {
                    e.Cancel = true;

                    result = new BrowserResult()
                    {
                        ResultType = BrowserResultType.Success,
                        Response = e.Uri.AbsoluteUri
                    };

                    signal.Release();

                    window.Close();
                }
            };

            window.Closing += (s, e) =>
            {
                signal.Release();
            };

            window.Content = webBrowser;
            window.Show();
            webBrowser.Source = new Uri(_options.StartUrl);

            await signal.WaitAsync();

            return result;
        }

        private bool BrowserIsNavigatingToRedirectUri(Uri uri)
        {
            return uri.AbsoluteUri.StartsWith(_options.EndUrl);
        }
    }

这就是我的称呼:

            //prompt login
            var options = new OidcClientOptions()
            {
                Authority = Current.Properties["IdentityServerAPIAddress"].ToString(),
                ClientId = "wpf",
                ClientSecret = "[secret]",
                Scope = "openid offline_access WebAPI",
                RedirectUri = "http://localhost/signin-oidc",
                Flow = OidcClientOptions.AuthenticationFlow.AuthorizationCode,
                Browser = new WpfEmbeddedBrowser()
            };

            _oidcClient = new OidcClient(options);

感谢您的帮助。谢谢。

0 个答案:

没有答案
相关问题