Exemple #1
0
        public async Task AcquireTokenInterative_WithValidCustomInstanceMetadata_Async()
        {
            string instanceMetadataJson = File.ReadAllText(
                ResourceHelper.GetTestResourceRelativePath("CustomInstanceMetadata.json"));

            using (var harness = CreateTestHarness())
            {
                // No instance discovery is made - it is important to not have this mock handler added
                // harness.HttpManager.AddInstanceDiscoveryMockHandler();

                PublicClientApplication app = PublicClientApplicationBuilder
                                              .Create(TestConstants.ClientId)
                                              .WithAuthority(new Uri("https://login.windows.net/common/"), false)
                                              .WithInstanceDiscoveryMetadata(instanceMetadataJson)
                                              .WithHttpManager(harness.HttpManager)
                                              .BuildConcrete();

                app.ServiceBundle.ConfigureMockWebUI(
                    AuthorizationResult.FromUri(app.AppConfig.RedirectUri + "?code=some-code"));

                // the rest of the communication with AAD happens on the preferred_network alias, not on login.windows.net
                harness.HttpManager.AddSuccessTokenResponseMockHandlerForPost(TestConstants.AuthorityCommonTenant);

                AuthenticationResult result = await app
                                              .AcquireTokenInteractive(TestConstants.s_scope)
                                              .ExecuteAsync(CancellationToken.None)
                                              .ConfigureAwait(false);

                Assert.IsNotNull(result);
                Assert.IsNotNull(result.Account);
                Assert.AreEqual(TestConstants.UniqueId, result.UniqueId);
                Assert.AreEqual(TestConstants.CreateUserIdentifier(), result.Account.HomeAccountId.Identifier);
                Assert.AreEqual(TestConstants.DisplayableId, result.Account.Username);
            }
        }
Exemple #2
0
        public async Task CustomInstanceDiscoveryMetadataUri_Async()
        {
            using (var harness = CreateTestHarness())
            {
                var customMetadataUri = new Uri("https://custom.instance.discovery.uri");
                var customAuthrority  = "https://my.custom.authority/common/";

                harness.HttpManager.AddInstanceDiscoveryMockHandler(
                    customAuthrority,
                    customMetadataUri,
                    TestConstants.DiscoveryJsonResponse.Replace("login.microsoftonline.com", "my.custom.authority"));

                PublicClientApplication app = PublicClientApplicationBuilder
                                              .Create(TestConstants.ClientId)
                                              .WithAuthority(customAuthrority, false)
                                              .WithInstanceDiscoveryMetadata(customMetadataUri)
                                              .WithHttpManager(harness.HttpManager)
                                              .BuildConcrete();

                MsalMockHelpers.ConfigureMockWebUI(
                    app.ServiceBundle.PlatformProxy,
                    AuthorizationResult.FromUri(app.AppConfig.RedirectUri + "?code=some-code"));

                // the rest of the communication with AAD happens on the preferred_network alias, not on login.windows.net
                harness.HttpManager.AddMockHandlerForTenantEndpointDiscovery(customAuthrority);
                harness.HttpManager.AddSuccessTokenResponseMockHandlerForPost(customAuthrority);

                AuthenticationResult result = await app
                                              .AcquireTokenInteractive(TestConstants.s_scope)
                                              .ExecuteAsync(CancellationToken.None)
                                              .ConfigureAwait(false);

                Assert.IsNotNull(result);
                Assert.IsNotNull(result.Account);
                Assert.AreEqual(TestConstants.UniqueId, result.UniqueId);
                Assert.AreEqual(TestConstants.CreateUserIdentifier(), result.Account.HomeAccountId.Identifier);
                Assert.AreEqual(TestConstants.DisplayableId, result.Account.Username);
            }
        }
Exemple #3
0
        private async Task CreateFailedAndThenSuccessfulResponseToVerifyErrorCodesInHttpTelemetryDataAsync(string errorCode)
        {
            using (var harness = CreateTestHarness())
            {
                harness.HttpManager.AddInstanceDiscoveryMockHandler();

                PublicClientApplication app = PublicClientApplicationBuilder.Create(TestConstants.ClientId)
                                              .WithAuthority(new Uri(ClientApplicationBase.DefaultAuthority), true)
                                              .WithHttpManager(harness.HttpManager)
                                              .WithTelemetry(new TraceTelemetryConfig())
                                              .BuildConcrete();
                // Interactive call and authentication fails
                var ui = new MockWebUI()
                {
                    MockResult = AuthorizationResult.FromUri(TestConstants.AuthorityHomeTenant + "?error=" + errorCode)
                };

                harness.HttpManager.AddMockHandlerForTenantEndpointDiscovery(TestConstants.AuthorityCommonTenant);
                MsalMockHelpers.ConfigureMockWebUI(app.ServiceBundle.PlatformProxy, ui);

                _correlationId = new Guid();
                AuthenticationResult result = null;

                try
                {
                    result = await app
                             .AcquireTokenInteractive(TestConstants.s_scope)
                             .WithCorrelationId(_correlationId)
                             .ExecuteAsync(CancellationToken.None)
                             .ConfigureAwait(false);
                }
                catch (MsalException exc)
                {
                    Assert.IsNotNull(exc);
                    Assert.AreEqual(errorCode, exc.ErrorCode);

                    // Try authentication again...
                    MsalMockHelpers.ConfigureMockWebUI(
                        app.ServiceBundle.PlatformProxy,
                        AuthorizationResult.FromUri(app.AppConfig.RedirectUri + "?code=some-code"));
                    var userCacheAccess = app.UserTokenCache.RecordAccess();

                    harness.HttpManager.AddSuccessfulTokenResponseWithHttpTelemetryMockHandlerForPost(
                        TestConstants.AuthorityCommonTenant,
                        null,
                        null,
                        CreateHttpTelemetryHeaders(
                            _correlationId,
                            TestConstants.InteractiveRequestApiId,
                            exc.ErrorCode,
                            TelemetryConstants.Zero));

                    result = app
                             .AcquireTokenInteractive(TestConstants.s_scope)
                             .WithCorrelationId(_correlationId)
                             .ExecuteAsync(CancellationToken.None)
                             .Result;

                    Assert.IsNotNull(result);
                    Assert.IsNotNull(result.Account);
                    Assert.AreEqual(TestConstants.UniqueId, result.UniqueId);
                    Assert.AreEqual(TestConstants.CreateUserIdentifier(), result.Account.HomeAccountId.Identifier);
                    Assert.AreEqual(TestConstants.DisplayableId, result.Account.Username);
                    userCacheAccess.AssertAccessCounts(0, 1);
                }
            }
        }