如何让 Identity Server 忘记凭据 - WPF

How to make Identity Server forget credentials - WPF

我有一个 WPF 应用程序,正在使用 Identity Server 4 进行身份验证。我使用 WpfEmbeddedBrowser class 登录。问题是每当我单击登录页面上的“记住我”复选框时,用户每次都会自动登录。这很好,但我不知道如何在用户单击注销按钮时让它停止。如何在 Identity Server 4 和 WPF 上注销用户?

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

这是我调用 WpfEmbeddedBrowser 的地方:

//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
            {
                result = await _oidcClient.LoginAsync();
            }
            catch (Exception ex)
            {
                //if this is thrown, it's probably because the login page loaded before the API
                MessageBox.Show("IdenitityServerAPI probably loaded before WPF. Try restarting.");
                Current.Shutdown();
                return;
            }

感谢任何帮助。谢谢

注销时您需要调用 Logout method of OidcClient。像这样的东西:

private async void LogoutButton_Click(object sender, EventArgs e)
{
    await _oidcClient.LogoutAsync();
}