首页 > 解决方案 > OIDCClient LogoutAsync 抛出 SemaphoreFullException

问题描述

我正在从 WPF 应用程序连接到 Identity Server 4,并且正在尝试执行注销。但是,在调用 _oidcClient.LogoutAsync() 时,我的 WpfEmbeddedBrowser 中出现了 SemaphoreFullException。最初我认为也许我不应该使用浏览器注销,但我认为这是不可能的。

这是调用登录和注销的部分:

//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();
            }
            ...

这是 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);
        }

标签: wpfidentityserver4semaphoreoidc-client

解决方案


设置maxCout2in SemaphoreSlim,这对我来说很合适。

var signal = new SemaphoreSlim(0, 2);

推荐阅读