Exemplo n.º 1
0
        internal static async Task <AuthenticationResult> TryLoginAsync(frmWebLogin webLoginFormCreatedOnUIThread)
        {
            var accounts = (await PublicClientApp.GetAccountsAsync()).ToList();

            if (accounts?.Count > 0)
            {
                await ClearAccountsAsync(accounts);
            }

            AuthenticationResult authResult = null;

            try
            {
                authResult = await PublicClientApp
                             .AcquireTokenSilent(Scopes, accounts.FirstOrDefault())
                             .ExecuteAsync();
            }
            catch (MsalUiRequiredException)
            {
                try
                {
                    authResult = await PublicClientApp.AcquireTokenInteractive(BlackBoardApplication.Scopes)
                                 .WithCustomWebUi(webLoginFormCreatedOnUIThread)
                                 .ExecuteAsync();
                }
                catch (System.Exception)
                {
                    // TODO: Implement error handling.
                }
            }

            if (authResult.AccessToken != null)
            {
                // Once the token has been returned by MSAL, add it to the http authorization header, before making the call to access the To Do list service.
                HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
                IsLoggedIn = true;
            }
            else
            {
                IsLoggedIn = false;
            }

            return(authResult);
        }
Exemplo n.º 2
0
        private async void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                var loginBrowser = new frmWebLogin();
                loginBrowser.Show();
                await loginBrowser.WaitForInitializeAsync();

                var result = await BlackBoardApplication.TryLoginAsync(loginBrowser);

                loginBrowser.Close();
                var userLoginInfo = await BlackBoardApplication.GetUserLoginInfoAsync();

                txtBlackboard.Text = userLoginInfo.Blackboard;
                lblLoginInfo.Text  = $"{userLoginInfo.Name} - ({userLoginInfo.UserId})";
            }
            catch (Exception)
            {
                throw;
            }
        }