コード例 #1
0
        public async Task <string> LoginWithRefreshTokenAsync()
        {
            List <string> scopesWithRefresh = scopes.ToList();

            scopesWithRefresh.Add("offline_access");

            IPublicClientApplication app = PublicClientApplicationBuilder
                                           .Create(clientId)
                                           .WithTenantId(tenantId)
                                           .WithRedirectUri(redirectUri)
                                           .Build();

            TokenCacheSerialization.EnableSerialization(app.UserTokenCache);

            AuthenticationResult result = null;
            var accounts = await app.GetAccountsAsync();

            if (accounts.Any())
            {
                try
                {
                    // attempt to get a token from the cache or silently refresh
                    result = await app.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
                             .ExecuteAsync();
                }
                catch (MsalUiRequiredException)
                {
                    // swallow exception
                }
            }

            // cache empty or no token for account
            if (result == null)
            {
                try
                {
                    result = await app.AcquireTokenInteractive(scopes)
                             .ExecuteAsync();
                }
                catch (MsalServiceException ex)
                {
                    throw;
                }
                catch (OperationCanceledException)
                {
                    result = null;
                }
                catch (MsalClientException ex)
                {
                    result = null;
                }
            }

            return(result.AccessToken);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: tlingenf/AzureAD-examples
        static async Task Main(string[] args)
        {
            // Build configuration
            configuration = new ConfigurationBuilder()
                            .SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName)
                            .AddJsonFile("appsettings.json", false)
                            .AddJsonFile("appsettings.json.user", true)
                            .Build();

            var defaultForegroundColor = Console.ForegroundColor;

            var example = new GrantTypeExamples(configuration);

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Select an authentication flow.");
            Console.WriteLine("1) - App Only Client Credentials Flow");
            Console.WriteLine("2) - Resource Owner Password Credentials");
            Console.WriteLine("3) - Device Code Flow");
            Console.WriteLine("4) - Interactive Login");
            Console.WriteLine("5) - Windows Integrated");
            Console.WriteLine("6) - Refresh Token");
            Console.WriteLine("7) - Write Token Cache");
            Console.WriteLine("8) - Application Certificate");

            var selection = Console.ReadKey();

            Console.WriteLine();

            string accessToken = null;

            try
            {
                switch (selection.KeyChar)
                {
                case '1':
                    accessToken = await example.ClientCredentialsAsync();

                    break;

                case '2':
                    accessToken = await example.ResourceOwnerPasswordAsync();

                    break;

                case '3':
                    accessToken = await example.DeviceCodeAsync();

                    break;

                case '4':
                    accessToken = await example.InteractiveAsync();

                    break;

                case '5':
                    accessToken = await example.IntegratedAsync();

                    break;

                case '6':
                    accessToken = await example.LoginWithRefreshTokenAsync();

                    break;

                case '7':
                    var tokenCache = TokenCacheSerialization.GetTokenCacheValue();
                    Console.WriteLine(JsonConvert.SerializeObject(tokenCache));
                    break;

                case '8':
                    accessToken = await example.CertificateAuthAsync();

                    break;
                }

                Console.WriteLine("Your access token is:");
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(accessToken);
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
            }

            Console.ForegroundColor = defaultForegroundColor;
        }