diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/README.md b/sdk/eventgrid/Azure.Messaging.EventGrid/README.md index 1cfe4ef231672..24a8c353c40c8 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/README.md +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/README.md @@ -121,7 +121,7 @@ List eventsList = new List new CloudEvent( "/cloudevents/example/binarydata", "Example.EventType", - new BinaryData("This is binary data"), + Encoding.UTF8.GetBytes("This is binary data"), "example/binary")}; // Send the events diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample1_PublishEventsToTopic.md b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample1_PublishEventsToTopic.md index a78ba7d26c963..c888c78c35406 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample1_PublishEventsToTopic.md +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample1_PublishEventsToTopic.md @@ -24,6 +24,15 @@ EventGridPublisherClient client = new EventGridPublisherClient( new AzureKeyCredential(topicAccessKey), clientOptions); ``` +Event Grid also supports authenticating with a shared access signature which allows for providing access to a resource that expires by a certain time without sharing your access key: +```C# Snippet:CreateWithSas +var builder = new EventGridSasBuilder(new Uri(topicEndpoint), DateTimeOffset.Now.AddHours(1)); +var keyCredential = new AzureKeyCredential(topicAccessKey); +var sasCredential = new AzureSasCredential(builder.GenerateSas(keyCredential)); +EventGridPublisherClient client = new EventGridPublisherClient( + new Uri(topicEndpoint), + sasCredential); +``` ## Publishing Events to Azure Event Grid ### Using `EventGridEvent` @@ -69,7 +78,7 @@ List eventsList = new List new CloudEvent( "/cloudevents/example/binarydata", "Example.EventType", - new BinaryData("This is binary data"), + Encoding.UTF8.GetBytes("This is binary data"), "example/binary")}; // Send the events diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs index 07808a3bd4c21..8599c5fac1610 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs @@ -47,6 +47,37 @@ public async Task SendEventGridEventsToTopic() #endregion } + [Test] + 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:CreateWithSas + var builder = new EventGridSasBuilder(new Uri(topicEndpoint), DateTimeOffset.Now.AddHours(1)); + var keyCredential = new AzureKeyCredential(topicAccessKey); + var sasCredential = new AzureSasCredential(builder.GenerateSas(keyCredential)); + EventGridPublisherClient client = new EventGridPublisherClient( + new Uri(topicEndpoint), + sasCredential); + #endregion + + // Add EventGridEvents to a list to publish to the topic + List eventsList = new List + { + new EventGridEvent( + "ExampleEventSubject", + "Example.EventType", + "1.0", + "This is the event data") + }; + + // Send the events + await client.SendEventsAsync(eventsList); + } + // This sample demonstrates how to publish CloudEvents 1.0 schema events to an Event Grid topic. [Test] public async Task SendCloudEventsToTopic() @@ -89,7 +120,7 @@ public async Task SendCloudEventsToTopic() new CloudEvent( "/cloudevents/example/binarydata", "Example.EventType", - new BinaryData("This is binary data"), + Encoding.UTF8.GetBytes("This is binary data"), "example/binary")}; // Send the events