public void CredentialConstructionClientCertificate()
        {
            string clientIdBackup = Environment.GetEnvironmentVariable("AZURE_CLIENT_ID");
            string tenantIdBackup = Environment.GetEnvironmentVariable("AZURE_TENANT_ID");
            string clientCertificateLocationBackup = Environment.GetEnvironmentVariable("AZURE_CLIENT_CERTIFICATE_PATH");

            try
            {
                Environment.SetEnvironmentVariable("AZURE_CLIENT_ID", "mockclientid");
                Environment.SetEnvironmentVariable("AZURE_TENANT_ID", "mocktenantid");
                Environment.SetEnvironmentVariable("AZURE_CLIENT_CERTIFICATE_PATH", "mockcertificatepath");

                var provider     = new EnvironmentCredential();
                var cred         = _credential(provider) as ClientCertificateCredential;
                var certProvider = cred.ClientCertificateProvider as ClientCertificateCredential.X509Certificate2FromFileProvider;

                Assert.NotNull(cred);
                Assert.NotNull(certProvider);
                Assert.AreEqual("mockclientid", cred.ClientId);
                Assert.AreEqual("mocktenantid", cred.TenantId);
                Assert.AreEqual("mockcertificatepath", certProvider.CertificatePath);
            }
            finally
            {
                Environment.SetEnvironmentVariable("AZURE_CLIENT_ID", clientIdBackup);
                Environment.SetEnvironmentVariable("AZURE_TENANT_ID", tenantIdBackup);
                Environment.SetEnvironmentVariable("AZURE_CLIENT_CERTIFICATE_PATH", clientCertificateLocationBackup);
            }
        }
        public void CredentialConstructionClientSecret()
        {
            string clientIdBackup     = Environment.GetEnvironmentVariable("AZURE_CLIENT_ID");
            string tenantIdBackup     = Environment.GetEnvironmentVariable("AZURE_TENANT_ID");
            string clientSecretBackup = Environment.GetEnvironmentVariable("AZURE_CLIENT_SECRET");

            try
            {
                Environment.SetEnvironmentVariable("AZURE_CLIENT_ID", "mockclientid");

                Environment.SetEnvironmentVariable("AZURE_TENANT_ID", "mocktenantid");

                Environment.SetEnvironmentVariable("AZURE_CLIENT_SECRET", "mockclientsecret");

                var provider = new EnvironmentCredential();

                ClientSecretCredential cred = _credential(provider) as ClientSecretCredential;

                Assert.NotNull(cred);

                Assert.AreEqual("mockclientid", cred.ClientId);

                Assert.AreEqual("mocktenantid", cred.TenantId);

                Assert.AreEqual("mockclientsecret", cred.ClientSecret);
            }
            finally
            {
                Environment.SetEnvironmentVariable("AZURE_CLIENT_ID", clientIdBackup);
                Environment.SetEnvironmentVariable("AZURE_TENANT_ID", tenantIdBackup);
                Environment.SetEnvironmentVariable("AZURE_CLIENT_SECRET", clientSecretBackup);
            }
        }
        private static async Task GetEnvironmentTokenCredential()
        {
            string[] scopes = new[] { "https://graph.microsoft.com/.default" };//see https://stackoverflow.com/questions/51781898/aadsts70011-the-provided-value-for-the-input-parameter-scope-is-not-valid/51789899

            EnvironmentCredential       environmentCredential       = new EnvironmentCredential();
            TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(environmentCredential, 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/users/[email protected]/");
            HttpResponseMessage response       = await httpClient.SendAsync(requestMessage);

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

            Console.WriteLine(jsonResponse);
        }
        public async Task TestInitializeFailed()
        {
            var credential = new EnvironmentCredential();
            var key        = new AadAccessKey(new Uri("http://localhost"), credential);

            var audience  = "http://localhost/chat";
            var claims    = Array.Empty <Claim>();
            var lifetime  = TimeSpan.FromHours(1);
            var algorithm = AccessTokenAlgorithm.HS256;

            var task = Assert.ThrowsAsync <AzureSignalRAccessTokenNotAuthorizedException>(
                async() => await key.GenerateAccessTokenAsync(audience, claims, lifetime, algorithm)
                );

            await Assert.ThrowsAnyAsync <Exception>(
                async() => await key.UpdateAccessKeyAsync()
                );

            await task;
        }
示例#5
0
        public void CredentialConstructionAuthLocation()
        {
            string authLocationBackup = Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION");
            string pathToFile         = Path.Combine(TestContext.CurrentContext.TestDirectory, "Data", "authfile.json");

            try
            {
                Environment.SetEnvironmentVariable("AZURE_AUTH_LOCATION", pathToFile);

                var provider            = new EnvironmentCredential();
                AuthFileCredential cred = _credential(provider) as AuthFileCredential;

                Assert.NotNull(cred);
                Assert.AreEqual(pathToFile, cred.FilePath);
            }
            finally
            {
                Environment.SetEnvironmentVariable("AZURE_AUTH_LOCATION", authLocationBackup);
            }
        }
        private static async Task GetChainedTokenCredential()
        {
            string[] scopes   = new[] { "User.Read" };
            string   clientId = "d662ac70-7482-45af-9dc3-c3cde8eeede4";
            EnvironmentCredential        environmentCredential = new EnvironmentCredential();
            InteractiveBrowserCredential myBrowserCredential   = new InteractiveBrowserCredential(clientId);

            TokenCredential []          tokenCredentials            = new TokenCredential[] { environmentCredential, myBrowserCredential };
            ChainedTokenCredential      chainedTokenCredential      = new ChainedTokenCredential(tokenCredentials);
            TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(chainedTokenCredential, 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/users/[email protected]/");
            HttpResponseMessage response       = await httpClient.SendAsync(requestMessage);

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

            Console.WriteLine(jsonResponse);
        }
        public async Task TestUpdateAccessKey()
        {
            var credential = new EnvironmentCredential();
            var endpoint   = "http://localhost";
            var key        = new AadAccessKey(new Uri(endpoint), credential);

            var audience  = "http://localhost/chat";
            var claims    = Array.Empty <Claim>();
            var lifetime  = TimeSpan.FromHours(1);
            var algorithm = AccessTokenAlgorithm.HS256;

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
            await Assert.ThrowsAsync <TaskCanceledException>(
                async() => await key.GenerateAccessTokenAsync(audience, claims, lifetime, algorithm, cts.Token)
                );

            var(kid, accessKey) = ("foo", SigningKey);
            key.UpdateAccessKey(kid, accessKey);

            var token = await key.GenerateAccessTokenAsync(audience, claims, lifetime, algorithm);

            Assert.NotNull(token);
        }
 public PartialMockDefaultAzureCredentialFactory(CredentialPipeline pipeline = null, EnvironmentCredential environmentCredential = null, ManagedIdentityCredential managedIdentityCredential = null) : base(pipeline ?? CredentialPipeline.GetInstance(null))
 {
     _environmentCredential     = environmentCredential;
     _managedIdentityCredential = managedIdentityCredential;
 }
 public static TokenCredential _credential(EnvironmentCredential provider)
 {
     return((TokenCredential)typeof(EnvironmentCredential).GetField("_credential", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(provider));
 }