Skip to content

Commit

Permalink
Update samples (Azure#18293)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshLove-msft authored and jongio committed Feb 9, 2021
1 parent 30f579f commit 393f4a1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sdk/eventgrid/Azure.Messaging.EventGrid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ List<CloudEvent> eventsList = new List<CloudEvent>
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -69,7 +78,7 @@ List<CloudEvent> eventsList = new List<CloudEvent>
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventGridEvent> eventsList = new List<EventGridEvent>
{
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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 393f4a1

Please sign in to comment.