Example #1
0
        public async Task <String> GetAccessTokenAsync(IReadOnlyList <string> scopes)
        {
            if (Type == ConnectedDevicesAccountType.MSA)
            {
                return(await MSAOAuthHelpers.GetAccessTokenUsingRefreshTokenAsync(Token, scopes));
            }
            else if (Type == ConnectedDevicesAccountType.AAD)
            {
                var authContext = new AuthenticationContext("https://login.microsoftonline.com/common");

                UserIdentifier       aadUserId = new UserIdentifier(Id, UserIdentifierType.UniqueId);
                AuthenticationResult result;
                try
                {
                    result = await authContext.AcquireTokenSilentAsync(scopes[0], Secrets.AAD_CLIENT_ID);
                }
                catch (Exception ex)
                {
                    Logger.Instance.LogMessage($"Token request failed: {ex.Message}");

                    // Token may have expired, try again non-silently
                    result = await authContext.AcquireTokenAsync(scopes[0], Secrets.AAD_CLIENT_ID,
                                                                 new Uri(Secrets.AAD_REDIRECT_URI), new PlatformParameters(PromptBehavior.Auto, true));
                }

                return(result.AccessToken);
            }
            else
            {
                throw new Exception("Invalid Account Type");
            }
        }
Example #2
0
        private async Task <ConnectedDevicesAccessTokenResult> GetMsaTokenForUserAsync(IReadOnlyList <string> scopes)
        {
            try
            {
                string accessToken = await MSAOAuthHelpers.GetAccessTokenUsingRefreshTokenAsync(MsaToken, scopes);

                Logger.Instance.LogMessage($"MSA Token : {accessToken}");
                return(new ConnectedDevicesAccessTokenResult(accessToken, ConnectedDevicesAccessTokenRequestStatus.Success));
            }
            catch (Exception ex)
            {
                Logger.Instance.LogMessage($"MSA Token request failed: {ex.Message}");
                return(new ConnectedDevicesAccessTokenResult(string.Empty, ConnectedDevicesAccessTokenRequestStatus.TransientError));
            }
        }