Skip to content

Commit 63055c1

Browse files
Add AAD sample to README. (Azure#22722)
* Add AAD sample to README. * Increase sleep time to allow permissions to propagate
1 parent b962442 commit 63055c1

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

sdk/eventgrid/Azure.Messaging.EventGrid/README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@ The access key can also be found through the [portal](https://docs.microsoft.com
3838
az eventgrid topic key list --name <your-resource-name> --resource-group <your-resource-group-name> --query "key1"
3939
```
4040

41-
#### Creating and Authenticating `EventGridPublisherClient`
41+
#### Authenticate using Topic Access Key
4242

4343
Once you have your access key and topic endpoint, you can create the publisher client as follows:
4444
```C#
4545
EventGridPublisherClient client = new EventGridPublisherClient(
4646
new Uri("<endpoint>"),
4747
new AzureKeyCredential("<access-key>"));
4848
```
49-
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.
49+
50+
#### Authenticate using Shared Access Signature
51+
52+
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.
5053
Generally, the workflow would be that one application would generate the SAS string and hand off the string to another application that would consume the string.
5154
Generate the SAS:
5255
```C# Snippet:GenerateSas
@@ -65,6 +68,18 @@ EventGridPublisherClient client = new EventGridPublisherClient(
6568

6669
`EventGridPublisherClient` also accepts a set of configuring options through `EventGridPublisherClientOptions`. For example, you can specify a custom serializer that will be used to serialize the event data to JSON.
6770

71+
#### Authenticate using Azure Active Directory
72+
73+
Azure Event Grid provides integration with Azure Active Directory (Azure AD) for identity-based authentication of requests. With Azure AD, you can use role-based access control (RBAC) to grant access to your Azure Event Grid resources to users, groups, or applications. The [Azure Identity library](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/identity/Azure.Identity/README.md) provides easy Azure Active Directory support for authentication.
74+
75+
To send events to a topic or domain using Azure Active Directory, the authenticated identity should have the "EventGrid Data Sender" role assigned.
76+
77+
```C# Snippet:EventGridAAD
78+
EventGridPublisherClient client = new EventGridPublisherClient(
79+
new Uri(topicEndpoint),
80+
new DefaultAzureCredential());
81+
```
82+
6883
## Key concepts
6984

7085
For information about general Event Grid concepts: [Concepts in Azure Event Grid](https://docs.microsoft.com/azure/event-grid/concepts).
@@ -264,7 +279,7 @@ foreach (CloudEvent cloudEvent in cloudEvents)
264279

265280
Using `TryGetSystemEventData()`:
266281

267-
If expecting mostly system events, it may be cleaner to switch on `TryGetSystemEventData()` and use pattern matching to act on the individual events. If an event is not a system event, the method will return false and the out parameter will be null.
282+
If expecting mostly system events, it may be cleaner to switch on `TryGetSystemEventData()` and use pattern matching to act on the individual events. If an event is not a system event, the method will return false and the out parameter will be null.
268283

269284
*As a caveat, if you are using a custom event type with an EventType value that later gets added as a system event by the service and SDK, the return value of `TryGetSystemEventData` would change from `false` to `true`. This could come up if you are pre-emptively creating your own custom events for events that are already being sent by the service, but have not yet been added to the SDK. In this case, it is better to use the generic `GetData<T>` method so that your code flow doesn't change automatically after upgrading (of course, you may still want to modify your code to consume the newly released system event model as opposed to your custom model).*
270285

@@ -323,7 +338,7 @@ You can also easily [enable console logging](https://github.com/Azure/azure-sdk-
323338
### Distributed Tracing
324339
The Event Grid library supports distributing tracing out of the box. In order to adhere to the CloudEvents specification's [guidance](https://github.com/cloudevents/spec/blob/master/extensions/distributed-tracing.md) on distributing tracing, the library will set the `traceparent` and `tracestate` on the `ExtensionAttributes` of a `CloudEvent` when distributed tracing is enabled. To learn more about how to enable distributed tracing in your application, take a look at the Azure SDK [distributed tracing documentation](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Diagnostics.md#Distributed-tracing).
325340

326-
### Event Grid on Kubernetes
341+
### Event Grid on Kubernetes
327342
This library has been tested and validated on Kubernetes using Azure Arc.
328343

329344
## Next steps

sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,31 @@ public async Task AuthenticateWithSasToken()
108108
await client.SendEventsAsync(eventsList);
109109
}
110110

111+
[Test]
112+
public async Task AuthenticateWithAAD()
113+
{
114+
string topicEndpoint = TestEnvironment.TopicHost;
115+
116+
#region Snippet:EventGridAAD
117+
EventGridPublisherClient client = new EventGridPublisherClient(
118+
new Uri(topicEndpoint),
119+
new DefaultAzureCredential());
120+
#endregion
121+
122+
// Add EventGridEvents to a list to publish to the topic
123+
List<EventGridEvent> eventsList = new List<EventGridEvent>
124+
{
125+
new EventGridEvent(
126+
"ExampleEventSubject",
127+
"Example.EventType",
128+
"1.0",
129+
"This is the event data")
130+
};
131+
132+
// Send the events
133+
await client.SendEventsAsync(eventsList);
134+
}
135+
111136
// This sample demonstrates how to publish CloudEvents 1.0 schema events to an Event Grid topic.
112137
[Test]
113138
public async Task SendCloudEventsToTopic()

sdk/eventgrid/test-resources-post.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ param (
1212
[string] $TestApplicationSecret
1313
)
1414

15-
Write-Verbose "Sleeping for 60 seconds to let RBAC replicate"
16-
Start-Sleep -s 60
15+
Write-Verbose "Sleeping for 120 seconds to let RBAC replicate"
16+
Start-Sleep -s 120

0 commit comments

Comments
 (0)