예제 #1
0
        public async Task GetRelayConfigurationAsync()
        {
            var connectionString = TestEnvironment.LiveTestDynamicConnectionString;

            var communicationIdentityClient = CreateInstrumentedCommunicationIdentityClient();
            Response <CommunicationUserIdentifier> response = await communicationIdentityClient.CreateUserAsync();

            var user = response.Value;

            #region Snippet:CreateCommunicationRelayClientAsync
            // Get a connection string to our Azure Communication resource.
            //@@var connectionString = "<connection_string>";
            var client = new CommunicationRelayClient(connectionString);
            #endregion Snippet:CreateCommunicationRelayClientAsync
            client = CreateClientWithConnectionString();

            #region Snippet:GetRelayConfigurationAsync
            Response <CommunicationRelayConfiguration> turnTokenResponse = await client.GetRelayConfigurationAsync(user);

            DateTimeOffset turnTokenExpiresOn = turnTokenResponse.Value.ExpiresOn;
            IReadOnlyList <CommunicationTurnServer> turnServers = turnTokenResponse.Value.TurnServers;
            Console.WriteLine($"Expires On: {turnTokenExpiresOn}");
            foreach (CommunicationTurnServer turnServer in turnServers)
            {
                foreach (string url in turnServer.Urls)
                {
                    Console.WriteLine($"TURN Url: {url}");
                }
                Console.WriteLine($"TURN Username: {turnServer.Username}");
                Console.WriteLine($"TURN Credential: {turnServer.Credential}");
            }
            #endregion Snippet:GetRelayConfigurationAsync
        }
예제 #2
0
        public async Task GetRelayConfigurationAsyncWithoutIdentity()
        {
            var connectionString = TestEnvironment.LiveTestDynamicConnectionString;

            // Get a connection string to our Azure Communication resource.
            //@@var connectionString = "<connection_string>";
            var client = new CommunicationRelayClient(connectionString);

            client = CreateClientWithConnectionString();

            #region Snippet:GetRelayConfigurationAsync
            Response <CommunicationRelayConfiguration> relayConfiguration = await client.GetRelayConfigurationAsync();

            DateTimeOffset turnTokenExpiresOn         = relayConfiguration.Value.ExpiresOn;
            IList <CommunicationIceServer> iceServers = relayConfiguration.Value.IceServers;
            Console.WriteLine($"Expires On: {turnTokenExpiresOn}");
            foreach (CommunicationIceServer iceServer in iceServers)
            {
                foreach (string url in iceServer.Urls)
                {
                    Console.WriteLine($"ICE Server Url: {url}");
                }
                Console.WriteLine($"ICE Server Username: {iceServer.Username}");
                Console.WriteLine($"ICE Server Credential: {iceServer.Credential}");
                Console.WriteLine($"ICE Server RouteType: {iceServer.RouteType}");
            }
            #endregion Snippet:GetRelayConfigurationAsync
        }
예제 #3
0
        public async Task CreateCommunicationRelayWithToken()
        {
            #region Snippet:CreateCommunicationRelayFromToken
            var endpoint = new Uri("https://my-resource.communication.azure.com");
            /*@@*/ endpoint = TestEnvironment.LiveTestDynamicEndpoint;
            TokenCredential tokenCredential = new DefaultAzureCredential();
            var             client          = new CommunicationRelayClient(endpoint, tokenCredential);
            #endregion Snippet:CreateCommunicationRelayFromToken

            var communicationIdentityClient = CreateInstrumentedCommunicationIdentityClient();
            Response <CommunicationUserIdentifier> response = await communicationIdentityClient.CreateUserAsync();

            var user = response.Value;

            client = CreateClientWithTokenCredential();
            try
            {
                Response <CommunicationRelayConfiguration> relayConfigurationResponse = await client.GetRelayConfigurationAsync(user);
            }
            catch (Exception ex)
            {
                Assert.Fail($"Unexpected error: {ex}");
            }
        }
예제 #4
0
        public async Task CreateCommunicationRelayWithAccessKey()
        {
            #region Snippet:CreateCommunicationRelayFromAccessKey
            var endpoint  = new Uri("https://my-resource.communication.azure.com");
            var accessKey = "<access_key>";
            /*@@*/ endpoint  = TestEnvironment.LiveTestDynamicEndpoint;
            /*@@*/ accessKey = TestEnvironment.LiveTestDynamicAccessKey;
            var client = new CommunicationRelayClient(endpoint, new AzureKeyCredential(accessKey));
            #endregion Snippet:CreateCommunicationRelayFromAccessKey

            var communicationIdentityClient = CreateInstrumentedCommunicationIdentityClient();
            Response <CommunicationUserIdentifier> response = await communicationIdentityClient.CreateUserAsync();

            var user = response.Value;

            client = CreateClientWithAzureKeyCredential();
            try
            {
                Response <CommunicationRelayConfiguration> relayConfigurationResponse = await client.GetRelayConfigurationAsync(user);
            }
            catch (Exception ex)
            {
                Assert.Fail($"Unexpected error: {ex}");
            }
        }
예제 #5
0
        public async Task CreateCommunicationRelayWithAccessKeyWithoutIdentity()
        {
            var endpoint  = new Uri("https://my-resource.communication.azure.com");
            var accessKey = "<access_key>";

            /*@@*/
            endpoint = TestEnvironment.LiveTestDynamicEndpoint;
            /*@@*/
            accessKey = TestEnvironment.LiveTestDynamicAccessKey;
            var client = new CommunicationRelayClient(endpoint, new AzureKeyCredential(accessKey));

            client = CreateClientWithAzureKeyCredential();
            try
            {
                Response <CommunicationRelayConfiguration> relayConfigurationResponse = await client.GetRelayConfigurationAsync();
            }
            catch (Exception ex)
            {
                Assert.Fail($"Unexpected error: {ex}");
            }
        }
        public async Task GettingTurnCredentialsWithRequestedRouteType(AuthMethod authMethod, RouteType routeType)
        {
            CommunicationRelayClient client = authMethod switch
            {
                AuthMethod.ConnectionString => CreateClientWithConnectionString(),
                AuthMethod.KeyCredential => CreateClientWithAzureKeyCredential(),
                AuthMethod.TokenCredential => CreateClientWithTokenCredential(),
                _ => throw new ArgumentOutOfRangeException(nameof(authMethod)),
            };

            CommunicationIdentityClient communicationIdentityClient = CreateInstrumentedCommunicationIdentityClient();

            Response <CommunicationUserIdentifier> userResponse = await communicationIdentityClient.CreateUserAsync();

            Response <CommunicationRelayConfiguration> turnCredentialsResponse = await client.GetRelayConfigurationAsync(userResponse.Value, routeType);

            Assert.IsNotNull(turnCredentialsResponse.Value);
            Assert.IsNotNull(turnCredentialsResponse.Value.ExpiresOn);
            Assert.IsNotNull(turnCredentialsResponse.Value.IceServers);
            foreach (CommunicationIceServer serverCredential in turnCredentialsResponse.Value.IceServers)
            {
                foreach (string url in serverCredential.Urls)
                {
                    Assert.IsFalse(string.IsNullOrWhiteSpace(url));
                }
                Assert.IsFalse(string.IsNullOrWhiteSpace(serverCredential.Username));
                Assert.IsFalse(string.IsNullOrWhiteSpace(serverCredential.Credential));
                Assert.AreEqual(routeType, serverCredential.RouteType);
            }
        }
        public async Task GettingTurnCredentialsAndValidateSerialization(AuthMethod authMethod, params string[] scopes)
        {
            CommunicationRelayClient client = authMethod switch
            {
                AuthMethod.ConnectionString => CreateClientWithConnectionString(),
                AuthMethod.KeyCredential => CreateClientWithAzureKeyCredential(),
                AuthMethod.TokenCredential => CreateClientWithTokenCredential(),
                _ => throw new ArgumentOutOfRangeException(nameof(authMethod)),
            };

            Response <CommunicationRelayConfiguration> turnCredentialsResponse = await client.GetRelayConfigurationAsync();

            Assert.IsNotNull(turnCredentialsResponse.Value);
            Assert.IsNotNull(turnCredentialsResponse.Value.ExpiresOn);
            Assert.IsNotNull(turnCredentialsResponse.Value.IceServers);

            var turnConfig           = turnCredentialsResponse.Value;
            var turnConfigSerialized = JsonSerializer.Serialize(turnConfig);

            Assert.IsNotNull(turnConfigSerialized);

            var turnConfigDeserialized = JsonSerializer.Deserialize <CommunicationRelayConfiguration>(turnConfigSerialized);

            Assert.IsNotNull(turnConfigDeserialized);

            foreach (CommunicationIceServer serverCredential in turnCredentialsResponse.Value.IceServers)
            {
                foreach (string url in serverCredential.Urls)
                {
                    Assert.IsFalse(string.IsNullOrWhiteSpace(url));
                }

                var serializedIceServer = JsonSerializer.Serialize(serverCredential);
                Assert.IsNotNull(serializedIceServer);

                CommunicationIceServer?deserializedIceServer = JsonSerializer.Deserialize <CommunicationIceServer>(serializedIceServer);

                Assert.IsNotNull(deserializedIceServer);
                Assert.IsNotNull(deserializedIceServer?.Username);
                Assert.IsNotNull(deserializedIceServer?.RouteType);
                Assert.IsNotNull(deserializedIceServer?.Urls);
                Assert.AreEqual(serverCredential.Username, deserializedIceServer?.Username);
                Assert.AreEqual(serverCredential.RouteType, deserializedIceServer?.RouteType);
                CollectionAssert.AreEqual(serverCredential.Urls, deserializedIceServer?.Urls);
            }
        }
        public async Task GettingTurnCredentialsGeneratesTurnCredentialsWithAllparameters(AuthMethod authMethod, params string[] scopes)
        {
            CommunicationRelayClient client = authMethod switch
            {
                AuthMethod.ConnectionString => CreateClientWithConnectionString(),
                AuthMethod.KeyCredential => CreateClientWithAzureKeyCredential(),
                AuthMethod.TokenCredential => CreateClientWithTokenCredential(),
                _ => throw new ArgumentOutOfRangeException(nameof(authMethod)),
            };

            CommunicationIdentityClient communicationIdentityClient = CreateInstrumentedCommunicationIdentityClient();

            Response <CommunicationUserIdentifier> userResponse = await communicationIdentityClient.CreateUserAsync();

            DateTimeOffset currentTime = DateTimeOffset.Now;
            Response <CommunicationRelayConfiguration> turnCredentialsResponse = await client.GetRelayConfigurationAsync(userResponse.Value, RouteType.Nearest, 5000);

            currentTime.AddSeconds(5000);

            Assert.IsNotNull(turnCredentialsResponse.Value);
            Assert.IsNotNull(turnCredentialsResponse.Value.ExpiresOn);

            // We only check if we are actually hitting an endpoint.
            // If we compare them when running from playback, the expiresOn value will be the one that was previously recorded so it will always fail.
            if (Mode != RecordedTestMode.Playback)
            {
                Assert.IsTrue(currentTime <= turnCredentialsResponse.Value.ExpiresOn);
            }

            Assert.IsNotNull(turnCredentialsResponse.Value.IceServers);

            foreach (CommunicationIceServer serverCredential in turnCredentialsResponse.Value.IceServers)
            {
                foreach (string url in serverCredential.Urls)
                {
                    Assert.IsFalse(string.IsNullOrWhiteSpace(url));
                }
                Assert.IsFalse(string.IsNullOrWhiteSpace(serverCredential.Username));
                Assert.IsFalse(string.IsNullOrWhiteSpace(serverCredential.Credential));
                Assert.AreEqual(RouteType.Nearest, serverCredential.RouteType);
            }
        }
        public async Task GettingTurnCredentialsGeneratesTurnCredentialsWithoutIdentity(AuthMethod authMethod, params string[] scopes)
        {
            CommunicationRelayClient client = authMethod switch
            {
                AuthMethod.ConnectionString => CreateClientWithConnectionString(),
                AuthMethod.KeyCredential => CreateClientWithAzureKeyCredential(),
                AuthMethod.TokenCredential => CreateClientWithTokenCredential(),
                _ => throw new ArgumentOutOfRangeException(nameof(authMethod)),
            };

            Response <CommunicationRelayConfiguration> turnCredentialsResponse = await client.GetRelayConfigurationAsync();

            Assert.IsNotNull(turnCredentialsResponse.Value);
            Assert.IsNotNull(turnCredentialsResponse.Value.ExpiresOn);
            Assert.IsNotNull(turnCredentialsResponse.Value.IceServers);

            foreach (CommunicationIceServer serverCredential in turnCredentialsResponse.Value.IceServers)
            {
                foreach (string url in serverCredential.Urls)
                {
                    Assert.IsFalse(string.IsNullOrWhiteSpace(url));
                }
                Assert.IsFalse(string.IsNullOrWhiteSpace(serverCredential.Username));
                Assert.IsFalse(string.IsNullOrWhiteSpace(serverCredential.Credential));
                Assert.IsNotNull((serverCredential.RouteType));
            }
        }