Exemple #1
0
        public async Task AuthenticateWithAutCodeHonorsRedirectUri([Values(null, redirectUriString)] string redirectUri)
        {
            var mockTransport = new MockTransport(req =>
            {
                if (redirectUri is not null && req.Uri.Path.EndsWith("/token"))
                {
                    var content = ReadMockRequestContent(req).GetAwaiter().GetResult();
                    Assert.That(WebUtility.UrlDecode(content), Does.Contain(redirectUri ?? string.Empty));
                }
                return(CreateMockMsalTokenResponse(200, expectedToken, TenantId, "foo"));
            });
            var options = new AuthorizationCodeCredentialOptions {
                Transport = mockTransport
            };

            if (redirectUri != null)
            {
                options.RedirectUri = new Uri(redirectUri);
            }
            options.Retry.MaxDelay = TimeSpan.Zero;
            var pipeline = CredentialPipeline.GetInstance(options);

            AuthorizationCodeCredential credential =
                InstrumentClient(new AuthorizationCodeCredential(TenantId, ClientId, clientSecret, authCode, options, null, pipeline));

            var context = new TokenRequestContext(new[] { Scope }, tenantId: TenantId);
            await credential.GetTokenAsync(context);
        }
        public async Task AuthenticateWithAuthCodeMockAsync()
        {
            var expectedToken = Guid.NewGuid().ToString();
            var authCode      = Guid.NewGuid().ToString();
            var clientId      = Guid.NewGuid().ToString();
            var tenantId      = Guid.NewGuid().ToString();
            var clientSecret  = Guid.NewGuid().ToString();

            MockResponse response = CreateAuthorizationResponse(expectedToken);

            var mockTransport = new MockTransport(request => ProcessMockRequest(request, tenantId, expectedToken));

            var options = new TokenCredentialOptions()
            {
                Transport = mockTransport
            };

            AuthorizationCodeCredential cred = InstrumentClient(new AuthorizationCodeCredential(tenantId, clientId, clientSecret, authCode, options));

            AccessToken token = await cred.GetTokenAsync(new TokenRequestContext(new string[] { "https://vault.azure.net/.default" }));

            Assert.AreEqual(token.Token, expectedToken);

            AccessToken token2 = await cred.GetTokenAsync(new TokenRequestContext(new string[] { "https://managemnt.azure.com/.default" }));

            Assert.AreEqual(token.Token, expectedToken);
        }
Exemple #3
0
        public async Task AuthenticateWithAuthCodeHonorsReplyUrl([Values(null, ReplyUrl)] string replyUri)
        {
            AuthorizationCodeCredentialOptions options = null;

            if (replyUri != null)
            {
                options = new AuthorizationCodeCredentialOptions {
                    RedirectUri = new Uri(replyUri)
                };
            }
            var context = new TokenRequestContext(new[] { Scope });

            expectedReplyUri = replyUri;

            AuthorizationCodeCredential cred = InstrumentClient(
                new AuthorizationCodeCredential(TenantId, ClientId, clientSecret, authCode, options, mockConfidentialMsalClient));

            AccessToken token = await cred.GetTokenAsync(context);

            Assert.AreEqual(token.Token, expectedToken, "Should be the expected token value");

            AccessToken token2 = await cred.GetTokenAsync(context);

            Assert.AreEqual(token2.Token, expectedToken, "Should be the expected token value");
        }
Exemple #4
0
        public async Task AuthenticateWithAuthCodeHonorsTenantId([Values(null, TenantIdHint)] string tenantId, [Values(true)] bool allowMultiTenantAuthentication)
        {
            var context = new TokenRequestContext(new[] { Scope }, tenantId: tenantId);

            expectedTenantId = TenantIdResolver.Resolve(TenantId, context);

            AuthorizationCodeCredential cred = InstrumentClient(
                new AuthorizationCodeCredential(TenantId, ClientId, clientSecret, authCode, options, mockConfidentialMsalClient));

            AccessToken token = await cred.GetTokenAsync(context);

            Assert.AreEqual(token.Token, expectedToken, "Should be the expected token value");

            AccessToken token2 = await cred.GetTokenAsync(context);

            Assert.AreEqual(token2.Token, expectedToken, "Should be the expected token value");
        }
        private static async Task GetAuthorizationCodeCredential()
        {
            string[] scopes   = new[] { "https://graph.microsoft.com/.default" };
            string   clientId = "317bd2d8-58b7-4be6-b5bc-d5567a6df8db";

            //TODO needs to be refreshed
            string authCode = "OAQABAAIAAABeAFzDwllzTYGDLh_qYbH8-fS3d_J-R8zEdrSFxP3SMigriWF5cdAiV5KkrWu2E_M5m-OBUZBftegjZymsS3dgFA1ZGorEmSwMjlRzrcyRDOMatyEspA8QnFy7-84aZIMGKPPaQ4FF6g2Ll5J4Jewk0lEKjBkWo1IY8Eja_kly0kuZDgOJyGao_5VJRJYFdcDRXOkwattPyY2v6MeL5dsRxTqzBUducnBA9D54jOkbxVehxLzyYaF7DWNC7teei-PzJ-DOhgAkiuIbtbDObFYvmQDnOLwwxvf3PRdQS_xqw79TxdFKNFMIbuwjIhtS-e_FjClLMZcHohrs11FcWo-fuTwMoQt14HbD9gt0aaxgCgy8CaLH7tJnDyDYGfJTriq1FXC1S76iWgxj_30teP9Ul01vliD1Rmi8hGiejHP2zN0J5RE3HGToDMnGLCbHFYGDiAM5Ju9L6o4QuijuI1UY2059RPKtwjy6P5eBJPUdROv8D7Qm9jSmy6pYH8IYPeVg1l1C6ALAgzDl3Q2RU0v37-i3xhBXz-ZpWQrXVXreeeZz5z5HS1oo28VuBsMV4KMHgIslQZ2vJw4XI4-EUyCfw6avx3Cgv2G22BtqyPGBi5Fm2nkaORdnxQcsp6OxpGvjSksSFmjV8F-12KmdEif_0__rtz7t2dmQrZ6Hg12uNSAA";
            AuthorizationCodeCredential authorizationCodeCredential = new AuthorizationCodeCredential("9cacb64e-358b-418b-967a-3cabc2a0ea95",
                                                                                                      clientId,
                                                                                                      "ahYBc9/Nejqg=b@GzXvo[2xlGgLHIq59",
                                                                                                      authCode);
            TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(authorizationCodeCredential, scopes);

            //Try to get something from the Graph!!
            HttpClient          httpClient     = GraphClientFactory.Create(tokenCredentialAuthProvider);
            HttpRequestMessage  requestMessage = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me/");
            HttpResponseMessage response       = await httpClient.SendAsync(requestMessage);

            //Print out the response :)
            string jsonResponse = await response.Content.ReadAsStringAsync();

            Console.WriteLine(jsonResponse);
        }