public async Task AuthenticateWithSasToken() { string topicEndpoint = TestEnvironment.TopicHost; string topicAccessKey = TestEnvironment.TopicKey; // Create the publisher client using an AzureKeyCredential // Custom topic should be configured to accept events of the Event Grid schema #region Snippet:GenerateSas var builder = new EventGridSasBuilder(new Uri(topicEndpoint), DateTimeOffset.Now.AddHours(1)); var keyCredential = new AzureKeyCredential(topicAccessKey); string sasToken = builder.GenerateSas(keyCredential); #endregion #region Snippet:AuthenticateWithSas var sasCredential = new AzureSasCredential(sasToken); EventGridPublisherClient client = new EventGridPublisherClient( new Uri(topicEndpoint), sasCredential); #endregion // Add EventGridEvents to a list to publish to the topic List <EventGridEvent> eventsList = new List <EventGridEvent> { new EventGridEvent( "ExampleEventSubject", "Example.EventType", "1.0", "This is the event data") }; // Send the events await client.SendEventsAsync(eventsList); }
public async Task CanPublishEventUsingSAS() { var builder = new EventGridSasBuilder( new Uri(TestEnvironment.TopicHost), DateTimeOffset.UtcNow.AddMinutes(60)); string sasToken = builder.GenerateSas(new AzureKeyCredential(TestEnvironment.TopicKey)); EventGridPublisherClient sasTokenClient = InstrumentClient( new EventGridPublisherClient( new Uri(TestEnvironment.TopicHost), new AzureSasCredential(sasToken), InstrumentClientOptions(new EventGridPublisherClientOptions()))); await sasTokenClient.SendEventsAsync(GetEventsList()); }