static void ClientCredentialsScenario() { ClientCredential clientCredential = new ClientCredential(ClientId, ConfigurationManager.AppSettings["clientSecret"]); TokenProvider tp = TokenProvider.CreateAadTokenProvider(new AuthenticationContext($"https://login.windows.net/{TenantId}"), clientCredential); var ehClient = EventHubClient.Create(new Uri($"sb://{EventHubNamespace}/"), EventHubName, tp); SendReceiveAsync(ehClient).Wait(); }
static void ClientAssertionCertScenario() { X509Certificate2 certificate = GetCertificate(); ClientAssertionCertificate clientAssertionCertificate = new ClientAssertionCertificate(ClientId, certificate); TokenProvider tp = TokenProvider.CreateAadTokenProvider(new AuthenticationContext($"https://login.windows.net/{TenantId}"), clientAssertionCertificate); var ehClient = EventHubClient.Create(new Uri($"sb://{EventHubNamespace}/"), EventHubName, tp); SendReceiveAsync(ehClient).Wait(); }
static void UserInteractiveLoginScenario() { TokenProvider tp = TokenProvider.CreateAadTokenProvider( new AuthenticationContext($"https://login.windows.net/{TenantId}"), ClientId, new Uri("http://eventhubs.microsoft.com"), new PlatformParameters(PromptBehavior.Auto), UserIdentifier.AnyUser ); var ehClient = EventHubClient.Create(new Uri($"sb://{EventHubNamespace}/"), EventHubName, tp); SendReceiveAsync(ehClient).Wait(); }
static void ClientCredentialsCertScenario() { ClientCredential clientCredential = new ClientCredential(ClientId, ConfigurationManager.AppSettings["clientSecret"]); MessagingFactorySettings messagingFactorySettings = new MessagingFactorySettings { TokenProvider = TokenProvider.CreateAadTokenProvider( new AuthenticationContext($"https://login.windows.net/{TenantId}"), clientCredential, ServiceAudience.EventHubsAudience ), TransportType = TransportType.Amqp }; SendReceive(messagingFactorySettings); }
static void UserInteractiveLoginScenario() { MessagingFactorySettings messagingFactorySettings = new MessagingFactorySettings { TokenProvider = TokenProvider.CreateAadTokenProvider( new AuthenticationContext($"https://login.windows.net/{TenantId}"), ClientId, new Uri("http://eventhubs.microsoft.com"), new PlatformParameters(PromptBehavior.SelectAccount), ServiceAudience.EventHubsAudience ), TransportType = TransportType.Amqp }; SendReceive(messagingFactorySettings); }
static void ClientAssertionCertScenario() { X509Certificate2 certificate = GetCertificate(); ClientAssertionCertificate clientAssertionCertificate = new ClientAssertionCertificate(ClientId, certificate); MessagingFactorySettings messagingFactorySettings = new MessagingFactorySettings { TokenProvider = TokenProvider.CreateAadTokenProvider( new AuthenticationContext($"https://login.windows.net/{TenantId}"), clientAssertionCertificate, ServiceAudience.EventHubsAudience ), TransportType = TransportType.Amqp }; SendReceive(messagingFactorySettings); }
async Task UserInteractiveLoginScenario() { MessagingFactorySettings messagingFactorySettings = new MessagingFactorySettings { TokenProvider = TokenProvider.CreateAadTokenProvider( new AuthenticationContext($"https://login.windows.net/{TenantId}"), ClientId, new Uri(ConfigurationManager.AppSettings["redirectURI"]), new PlatformParameters(PromptBehavior.SelectAccount), ServiceAudience.ServiceBusAudience ), TransportType = TransportType.Amqp }; await SendReceive(messagingFactorySettings); }
static void UserPasswordCredentialScenario() { UserPasswordCredential userPasswordCredential = new UserPasswordCredential( ConfigurationManager.AppSettings["userName"], ConfigurationManager.AppSettings["password"] ); MessagingFactorySettings messagingFactorySettings = new MessagingFactorySettings { TokenProvider = TokenProvider.CreateAadTokenProvider( new AuthenticationContext($"https://login.windows.net/{TenantId}"), ClientId, userPasswordCredential, ServiceAudience.EventHubsAudience ), TransportType = TransportType.Amqp }; SendReceive(messagingFactorySettings); }
public async Task UseITokenProviderWithAad() { var tenantId = ""; var aadAppId = ""; var aadAppSecret = ""; if (string.IsNullOrEmpty(tenantId)) { TestUtility.Log($"Skipping test during scheduled runs."); return; } var authContext = new AuthenticationContext($"https://login.windows.net/{tenantId}"); var cc = new ClientCredential(aadAppId, aadAppSecret); var tokenProvider = TokenProvider.CreateAadTokenProvider(authContext, cc); // Create new client with updated connection string. var csb = new EventHubsConnectionStringBuilder(TestUtility.EventHubsConnectionString); var ehClient = EventHubClient.Create(csb.Endpoint, csb.EntityPath, tokenProvider); // Send one event TestUtility.Log($"Sending one message."); var ehSender = ehClient.CreatePartitionSender("0"); var eventData = new EventData(Encoding.UTF8.GetBytes("Hello EventHub!")); await ehSender.SendAsync(eventData); // Receive event. PartitionReceiver ehReceiver = null; try { TestUtility.Log($"Receiving one message."); ehReceiver = ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, "0", EventPosition.FromStart()); var msg = await ehReceiver.ReceiveAsync(1); Assert.True(msg != null, "Failed to receive message."); } finally { await ehReceiver?.CloseAsync(); } }
async Task UseITokenProviderWithAad() { var tenantId = ""; var aadAppId = ""; var aadAppSecret = ""; if (string.IsNullOrEmpty(tenantId)) { TestUtility.Log($"Skipping test during scheduled runs."); return; } var authContext = new AuthenticationContext($"https://login.windows.net/{tenantId}"); var cc = new ClientCredential(aadAppId, aadAppSecret); var tokenProvider = TokenProvider.CreateAadTokenProvider(authContext, cc); // Create new client with updated connection string. var csb = new ServiceBusConnectionStringBuilder(TestUtility.NamespaceConnectionString); var queueClient = new QueueClient(csb.Endpoint, csb.EntityPath, tokenProvider); // Send and receive messages. await this.PeekLockTestCase(queueClient.InnerSender, queueClient.InnerReceiver, 10); }