From f6dbdda3c766ce1236ada0efb70cf24d0f8ed238 Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Fri, 29 Jan 2021 13:06:59 -0800 Subject: [PATCH] Update samples --- .../Azure.Messaging.EventGrid/README.md | 2 +- .../samples/Sample1_PublishEventsToTopic.md | 11 ++++++- .../Sample1_SendEventsToTopicAndDomain.cs | 33 ++++++++++++++++++- 3 files changed, 43 insertions(+), 3 deletions(-) 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