Example #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);
        }
Example #2
0
        /// <summary>
        /// Get Token for User.
        /// </summary>
        /// <returns>Token for user.</returns>
        public async Task <string> GetTokenForUserAsync(Connection connection, IEnumerable <string> scopes)
        {
            if (AccessTokenInfo?.AccessToken == null || AccessTokenInfo.ExpiresOn.Subtract(DateTimeOffset.UtcNow) > TimeSpan.Zero)
            {
                IEnumerable <IAccount> account = await PublicClientApp.GetAccountsAsync();

                var requriedscopes = new List <string>();
                try
                {
                    AccessTokenInfo = await PublicClientApp.AcquireTokenSilent(scopes, account.FirstOrDefault(s => s.Username == "navateja")).ExecuteAsync();
                }
                //To posiblities
                // 1- Accounts Dosn't Exist
                // 2- Microsoft.Identity.Client.MsalUiRequiredException
                catch (Exception)
                {
                    //await GetTokenByRefereshToken(scopes, "93ded07f-9eff-40f0-91b9-eb61022eeced.8a92f134-8bf1-47b2-9e5e-7c1653e22f85-login.windows.net-refreshtoken-2bae50d2-90aa-4b11-aca4-3caf162934f9--");

                    AccessTokenInfo = await PublicClientApp.AcquireTokenInteractive(scopes).WithAuthority("https://login.microsoftonline.com/7a096f3f-68c6-450a-9281-1e29226ebda9").WithPrompt(Microsoft.Identity.Client.Prompt.Consent).ExecuteAsync();
                }
            }
            return(AccessTokenInfo.AccessToken);
        }