暂停编程执行,直到MSAL完成授权

时间:2018-06-26 21:00:27

标签: msal onenote-api

我有一个使用MSAL授权的UWP应用。我希望该应用在执行任何其他代码之前先获取A​​cquireToken。现在,在运行代码并且未获取令牌之后,将弹出“用户名/密码”字段。我必须退出程序并重新启动。下次启动时,将使用GetTokenSilent选项,并且将应用程序设置为使用令牌。

我想取消此“重新启动”步骤,因为它是“用户惩罚”。

这是我在应用程序开头的代码。

  public MainPage()
    {
        this.InitializeComponent();

        gui = this;

        SignInButton_Click(null, null);

        Task task = CopyDatabase();

        CreateNewChartButton.Visibility = Visibility.Collapsed;

        DataSetup();


    }

这是登录代码:

 private async void SignInButton_Click(object sender, RoutedEventArgs e)
    {
        // StatusMsg.Text = string.Empty;

        try
        {
            authResult = await App.PublicClientApp.AcquireTokenSilentAsync(scopes, App.PublicClientApp.Users.FirstOrDefault());
            StatusMsg.Text = "Success Logging In";
        }
        catch (MsalUiRequiredException ex)
        {
            // A MsalUiRequiredException happened on AcquireTokenSilentAsync. This indicates you need to call AcquireTokenAsync to acquire a token
            System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");

            try
            {
                authResult = await App.PublicClientApp.AcquireTokenAsync(scopes);
                StatusMsg.Text = "Success Logging In with Scopes";
            }
            catch (MsalException msalex)
            {
                StatusMsg.Text = $"Error Acquiring Token:{System.Environment.NewLine}{msalex}";
            }
        }
        catch (Exception ex)
        {
            StatusMsg.Text = $"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}";
            return;
        }

        if (authResult != null)
        {
            StatusMsg.Text = "Signed In  ";
            StatusMsg.Text += $"Name: {authResult.User.Name}" + Environment.NewLine;
            StatusMsg.Text += $"Username: {authResult.User.DisplayableId}" + Environment.NewLine;
            StatusMsg.Text += $"Token Expires: {authResult.ExpiresOn.ToLocalTime()}" + Environment.NewLine;
            StatusMsg.Text += "NotebookName/ID = " + Globals.CurentNotebookName.ToString() + " / " + Globals.CurrentNotebookId.ToString() + Environment.NewLine;
        }
    }

我正在考虑添加ContentDialog吗?

0 个答案:

没有答案