当 auth0 必须与基于 windows 表单的应用程序集成时,如何获取 auth0 登录屏幕而不是 windows 表单 1(4.7.2)

问题描述 投票:0回答:1

我们试图了解 auth0 如何与 .NET 4.7.2 中基于 Windows 窗体的应用程序集成。

我已经从 Auth0 网站下载了示例代码。当您运行该应用程序时,我将看到一个表单。之后,我需要选择连接,然后将显示 Auth0 的登录屏幕。请参考下图

我想要的是当应用程序启动时,我希望首先显示 Auth0 登录屏幕,一旦凭据良好,就应该加载表单。

我不确定这是否可能。

我一直在互联网上检查并尝试了一些事情。但到目前为止还没有成功。

任何指示都会非常有帮助。

提前致谢。 此致, SP

windows winforms auth0 .net-4.7.2
1个回答
0
投票

Auth0提供的示例代码修改如下:-

public Form1()
        {
           
            InitializeComponent();
            this.Load += new EventHandler(this.Form1_Load); // addition
            
        }

private async void Form1_Load(object sender, EventArgs e)
        {
            this.Visible = false;
            this.ShowInTaskbar = false;
            string domain = ConfigurationManager.AppSettings["Auth0:Domain"];
            string clientId = ConfigurationManager.AppSettings["Auth0:ClientId"];
            client = new Auth0Client(new Auth0ClientOptions
            {
                Domain = domain,
                ClientId = clientId,
                // EnableTelemetry = true,

            });
            var extraParameters = new Dictionary<string, string>();

            extraParameters.Add("connection", "ConnectionParam");

            extraParameters.Add("audience", "");



            DisplayResult(await client.LoginAsync());

        }


private void DisplayResult(LoginResult loginResult)
        {
            this.Visible = true;
            this.ShowInTaskbar = true;
            this.Show();
            // Display error
            if (loginResult.IsError)
            {
                resultTextBox.Text = loginResult.Error;
                return;
            }
            
            loginButton.Visible = false;
            logoutButton.Visible = true;

            // Display result
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Tokens");
            sb.AppendLine("------");
            sb.AppendLine($"id_token: {loginResult.IdentityToken}");
            sb.AppendLine($"access_token: {loginResult.AccessToken}");
            sb.AppendLine($"refresh_token: {loginResult.RefreshToken}");
            sb.AppendLine();

            sb.AppendLine("Claims");
            sb.AppendLine("------");
            foreach (var claim in loginResult.User.Claims)
            {
                sb.AppendLine($"{claim.Type}: {claim.Value}");
            }
            resultTextBox.Text = sb.ToString();
            this.Focus();
        }

private async void LogoutButton_Click(object sender, EventArgs e)
        {
            BrowserResultType browserResult = await client.LogoutAsync();

            if (browserResult != BrowserResultType.Success)
            {
                resultTextBox.Text = browserResult.ToString();
                return;
            }

            logoutButton.Visible = false;
            loginButton.Visible = true;

            resultTextBox.Text = "";
            audienceTextBox.Text = "";
            connectionNameComboBox.Text = "";

            this.Close();
        }

问题就这样解决了。

© www.soinside.com 2019 - 2024. All rights reserved.