0

I found a class online that I use for logging in to my Identity Server API from my WPF. I need to delete its cookies programmatically. The code I keep finding elsewhere online seems to be relating to clearing cookies on a WebBrowser control that is specifically in the WPF window. I haven't gotten any of those to work so maybe it is different.

Here is the 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);
        }
    }

Here is how I call it:

            //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);

Any help is appreciated. Thanks.

Ethan Shoe
  • 466
  • 1
  • 7
  • 20
  • Where is the code that deletes the cookies, which part here doesnt work, what do you mean by doesnt work? – o_w Jul 28 '20 at 13:14
  • @o_w That is what I am trying to find out. The sample code is current working code for the purposes of opening a login window. I need to know how to delete the cookies from WpfEmbeddedBrowser. What doesn't work are the other related solutions on this site. – Ethan Shoe Jul 28 '20 at 13:18
  • But here I see you build a completely new Window with a completely new browser each time. You also call Window.Show() twice for some reason. – o_w Jul 28 '20 at 13:24
  • @o_w yes. That's the thing, I don't know why the cookie data is being kept when I am calling a new WpfEmbeddedBrowser each time. I'm not 100% sure about the Window.Show() thing. – Ethan Shoe Jul 28 '20 at 13:27
  • What in all of this makes you think that your problem is specifically the cookies in the browser? What do you see? What did you get when you opened it the second time? – o_w Jul 29 '20 at 06:57
  • @o_w the issue I'm having relates to the "remember me" button on the login page. I need to clear the cookies so that it no longer automatically logs in. – Ethan Shoe Jul 29 '20 at 12:59
  • I think this is duplicate of: https://stackoverflow.com/questions/37806321/how-to-clear-webbrowser-cache-in-wpf-application maybe this will help. – o_w Jul 29 '20 at 13:43

0 Answers0