Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update samples #18293

Merged
merged 1 commit into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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