Example #1
0
 internal static async Task ClearAccountsAsync(IEnumerable <IAccount> accounts)
 {
     foreach (var account in accounts)
     {
         await PublicClientApp.RemoveAsync(account);
     }
 }
Example #2
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 #3
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);
        }
Example #4
0
        public async Task <string> GetToken(Connection connection, IEnumerable <string> scopes)
        {
            IEnumerable <IAccount> account = await PublicClientApp.GetAccountsAsync();

            var requriedscopes = new List <string>();

            try
            {
                string redirectUri = "urn:ietf:wg:oauth:2.0:oob";// "urn://teamsgraph";// ; https://graph.microsoft.com
                ADAccessTokenInfo = await AuthenticationContext.AcquireTokenAsync("https://management.azure.com", "829d83fb-a70b-4d61-8939-ed1e016da9f0", new Uri(redirectUri), new PlatformParameters(PromptBehavior.SelectAccount));

                ADAccessTokenInfo = await AuthenticationContext.AcquireTokenSilentAsync("https://rohanpvtlimited.sharepoint.com", "2bae50d2-90aa-4b11-aca4-3caf162934f9");

                ADAccessTokenInfo = await AuthenticationContext.AcquireTokenSilentAsync("https://spmig.sharepoint.com", "829d83fb-a70b-4d61-8939-ed1e016da9f0");

                var AccessTokenInfo = await PublicClientApp.AcquireTokenInteractive(scopes).WithPrompt(Prompt.Consent).ExecuteAsync();

                var appRt  = PublicClientApp as IByRefreshToken;
                var result = appRt.AcquireTokenByRefreshToken(scopes, "AQABAAAAAACQN9QBRU3jT6bcBQLZNUj7UT3OmtlJb-jtX3aLB2awIN_bcmYpBZcImfJgeq80Mv9vtwZk53Je0efqAN1hO8Dneo4RjWqTcwaiqcyXR1ZLpMh2bIc7mPJOKTtulLG1-iCXZTvdcunNIvbsLYAi5SSzMf3xZ_JsfA4bwXkR-HCRlOkZZW-K12g36MMQDKNW4ktFs5qHxY6FMq2Q6CBD_uRSpz26yTgtVds_aLMNM6FVu2ZUFZUlxyBoAtzOrEEG_Cm94o9xhfgt6WMTwmp-_PD5FS-elP23XIBnoEy_w0L9DnHCfwuoT6qrio_hN3fMbzLNHOSxATGfyE9nf0OCXyqNW03gNFxJphnzoE4x6L4xZ0o_ErzH1LmSdaHfjrfYFumEoKr0EiQMvWtCIgBM6qHFiZ3aTrYj6cm_F_KmOxGWheQEKLjds56lvawtxtCacmkO5knVylXKcC8RsUjwEVjLpMWfKFXOWDcHJjTvWKNl7_HEVzGhVNfj7pP8mIaxyghcgduTH47K3iK2nhmy5mhUzgT5MhvJlqeptxLhFWw5LfZWTFkh_qp3mmmvvM8lvrV5KmlyKrGVsAqctXUzeF5WK9nycYqtMQb-jv9V8QewmyEZEliExlHloMsBePHIDy0uZK4bqABecq__cMqY_e8Z36H77biaFqwqT2U3aAQPc5eD3vcuG-o7itP738RF5mfSsaCsmWdsYeuOUVVrjhP3_L_Ve-_c3aZodFRreMQAi3MmrISkGytzG7xDZAFpJ5bCjecKv6fLR5A2Xeoje0oi-aGxiVNLI_Cq98Rt_dE03fVp2OJFhdQumEh5OJ20IBdU0tQHj02bzZwMw__A4UYCu2jN9DcFcSJTdWzdA_H0owJdmaMIccb4vGYDbSFLaN9VLkSI6FcYA_SFxp1Gh0yGgULPpJzORziyRaS5OTi-bSAA").ExecuteAsync().ConfigureAwait(false);
                var s      = result.GetAwaiter().GetResult();

                return(ADAccessTokenInfo.AccessToken);
            }
            //To posiblities
            // 1- Accounts Dosn't Exist
            // 2- Microsoft.Identity.Client.MsalUiRequiredException
            catch (Exception ex)
            {
                //await GetTokenByRefereshToken(scopes, "93ded07f-9eff-40f0-91b9-eb61022eeced.8a92f134-8bf1-47b2-9e5e-7c1653e22f85-login.windows.net-refreshtoken-2bae50d2-90aa-4b11-aca4-3caf162934f9--");

                //ADAccessTokenInfo = await AuthenticationContext.AcquireTokenAsync("https://spmig.sharepoint.com", "2bae50d2-90aa-4b11-aca4-3caf162934f9", new Uri(redirectUri), new PlatformParameters(PromptBehavior.SelectAccount));
                string redirectUri = "urn:ietf:wg:oauth:2.0:oob";
                ADAccessTokenInfo = await AuthenticationContext.AcquireTokenAsync("https://rohanpvtlimited.sharepoint.com", "2bae50d2-90aa-4b11-aca4-3caf162934f9", new Uri(redirectUri), new PlatformParameters(PromptBehavior.SelectAccount));

                return(ADAccessTokenInfo.AccessToken);
            }
        }