1

I'm connecting to Identity Server 4 from a WPF application and I am trying to perform a logout. However, I get a SemaphoreFullException in my WpfEmbeddedBrowser when calling _oidcClient.LogoutAsync(). Initally I thought maybe I shouldn't use the browser for logging out, but I don't think that is possible.

Here is the part calling the login and logout:

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

            try
            {
                loginResult = await _oidcClient.LoginAsync();
                logoutResult = await _oidcClient.LogoutAsync();
            }
            ...

Here is the WpfEmbeddedBrowser:

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);
        }
Ethan Shoe
  • 292
  • 2
  • 16

1 Answers1

0

Set maxCout to 2 in SemaphoreSlim, it's wroks for me.

var signal = new SemaphoreSlim(0, 2);
Mohammad Roshani
  • 338
  • 4
  • 15