|
| 1 | +--- |
| 2 | +author: pgrandhi |
| 3 | +ms.service: azure-communication-services |
| 4 | +ms.topic: include |
| 5 | +ms.date: 01/28/2024 |
| 6 | +ms.author: pgrandhi |
| 7 | +--- |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/dotnet/). |
| 12 | +- The latest version [.NET Core SDK](https://dotnet.microsoft.com/download/dotnet-core) for your operating system. |
| 13 | +- Get the latest version of the [.NET Microsoft Azure Event Grid Management SDK](/azure/event-grid/sdk-overview). |
| 14 | +- Get the latest version of the [Azure Identity](/dotnet/api/overview/azure/identity-readme) library. |
| 15 | +- An [Azure Communication Services resource](../../create-communication-resource.md). |
| 16 | + |
| 17 | +[!INCLUDE [register-event-grid-resource-provider.md](register-event-grid-resource-provider.md)] |
| 18 | + |
| 19 | +## Installing the SDK |
| 20 | + |
| 21 | +First, install the Microsoft Azure Event Grid Management library for .NET with [NuGet](https://www.nuget.org/): |
| 22 | + |
| 23 | +```csharp |
| 24 | +dotnet add package Azure.ResourceManager.EventGrid; |
| 25 | +``` |
| 26 | +Include the Event Grid Management SDK in your C# project: |
| 27 | + |
| 28 | +```csharp |
| 29 | +using Microsoft.Azure.Management.EventGrid; |
| 30 | +using Microsoft.Azure.Management.EventGrid.Models; |
| 31 | +``` |
| 32 | + |
| 33 | +## Authenticating with Azure Identity library |
| 34 | + |
| 35 | +### Prerequisites: |
| 36 | +1. Create a Microsoft Entra application and Service Principal and set up a client secret or trusted certificate issued by certificate authority by following the instructions [here](/entra/identity-platform/howto-create-service-principal-portal). |
| 37 | +1. Store the secret or the certificate in the Azure Keyvault. |
| 38 | +1. Give contributor or owner access to the subscription to that application following the instructions [here](/azure/role-based-access-control/quickstart-assign-role-user-portal). |
| 39 | +1. Read more about authorizing access to Event Grid resources [here](/azure/event-grid/security-authorization). |
| 40 | + |
| 41 | +The Azure Identity library provides Microsoft Entra ID (Formerly Azure Active Directory) token authentication support across the Azure SDK. It provides a set of TokenCredential implementations, which can be used to construct Azure SDK clients that support Microsoft Entra token authentication. You can read more about it [here](/dotnet/api/overview/azure/identity-readme). |
| 42 | + |
| 43 | +1. Include the Azure Identity client library for .NET with [NuGet](https://www.nuget.org/): |
| 44 | + |
| 45 | +```csharp |
| 46 | +dotnet add package Azure.Identity; |
| 47 | +dotnet add package Azure.Security.KeyVault.Secrets |
| 48 | +``` |
| 49 | +2. Include the Azure Identity library in your C# project |
| 50 | + |
| 51 | +```csharp |
| 52 | +using Microsoft.Azure.Identity; |
| 53 | +using Azure.Security.KeyVault.Secrets |
| 54 | +``` |
| 55 | + |
| 56 | +3. You can either pass the secret credentials or certificate credentials to get access token, based on how your Service Principal is configured. |
| 57 | + |
| 58 | + * Get access token using secret credential |
| 59 | + |
| 60 | + To get the secret credentials, you need to read it from the Keyvault you created in Prerequisite #2 using [SecretClient](/azure/key-vault/secrets/quick-create-net). |
| 61 | + |
| 62 | + ```csharp |
| 63 | + // Authenticate the Keyvault client with DefaultAzureCredential and get the secret. |
| 64 | + SecretClient secretClient = new SecretClient(new Uri("https://myvault.vault.azure.net/"), new DefaultAzureCredential()); |
| 65 | + string clientSecret = await secretClient.GetSecretAsync(secretName).Value; |
| 66 | + |
| 67 | + // Get access token using secret credentials |
| 68 | + string[] scopes = { "https://management.azure.com/.default" }; |
| 69 | + var application = ConfidentialClientApplicationBuilder |
| 70 | + .Create('your-servicePrincipal-appId') |
| 71 | + .WithAuthority(authorityUri: new Uri(authority), validateAuthority: true) |
| 72 | + .WithTenantId('your-tenant_id') |
| 73 | + .WithClientSecret(clientSecret) |
| 74 | + .Build(); |
| 75 | + |
| 76 | + var token = await application |
| 77 | + .AcquireTokenForClient(scopes) |
| 78 | + .ExecuteAsync(); |
| 79 | + ``` |
| 80 | + |
| 81 | + * Get access token using certificate credential |
| 82 | + |
| 83 | + To get the certificate credentials, you need to read it from the Keyvault you created in Prerequisite #2 using [CertificateClient](/azure/key-vault/certificates/quick-create-net). |
| 84 | + |
| 85 | + Read more about the Microsoft Entra application configuration authority [here](/entra/identity-platform/msal-client-application-configuration) |
| 86 | + |
| 87 | + ```csharp |
| 88 | + // Authenticate the certificate client with DefaultAzureCredential and get the certificate. |
| 89 | + CertificateClient certificateClient = new SecretClient(new Uri("https://myvault.vault.azure.net/"), new DefaultAzureCredential()); |
| 90 | + X509Certificat2 cert = await certificateClient.DownloadCertificateAsync(certificateName); |
| 91 | + |
| 92 | + // Get access token using certificate credentials |
| 93 | + string[] scopes = { "https://management.azure.com/.default" }; |
| 94 | + string authority = "https://login.microsoftonline.com/<tenant>/"; |
| 95 | + var application = ConfidentialClientApplicationBuilder |
| 96 | + .Create('<servicePrincipal-appId>') |
| 97 | + .WithAuthority(authorityUri: new Uri(authority), validateAuthority: true) |
| 98 | + .WithTenantId("<tenantId>") |
| 99 | + .WithCertificate(cert) |
| 100 | + .Build(); |
| 101 | + |
| 102 | + var token = await application |
| 103 | + .AcquireTokenForClient(scopes) |
| 104 | + .WithSendX5C(true) |
| 105 | + .ExecuteAsync(); |
| 106 | + ``` |
| 107 | + |
| 108 | +4. Authenticate EventGridManagementClient with access token using secret or certificate credentials |
| 109 | + |
| 110 | + |
| 111 | +```csharp |
| 112 | +// Authenticate EventGridManagementClient with Microsoft Entra ID access token credential |
| 113 | +eventGridClient = new EventGridManagementClient(new Uri("https://management.azure.com/"), |
| 114 | + new TokenCredentials(token.AccessToken)); |
| 115 | + |
| 116 | +eventGridClient.SubscriptionId = 'your_subscripiton_id'; |
| 117 | +``` |
| 118 | + |
| 119 | +## Create Event Subscription |
| 120 | +This code sample shows how to create the event subscription for the webhook subscriber endpoint. |
| 121 | + |
| 122 | +```csharp |
| 123 | +string webhookUri = $"<webhookUri>"; |
| 124 | +string resourceId="/subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Communication/CommunicationServices/<acsResourceName>"; |
| 125 | +string[] includedEventTypes = new string[]{ "Microsoft.Communication.SMSReceived", |
| 126 | + "Microsoft.Communication.SMSDeliveryReportReceived" |
| 127 | + }; |
| 128 | + |
| 129 | +EventSubscription eventSubscription = new EventSubscription( |
| 130 | + name: "<eventSubscriptionName>", |
| 131 | + eventDeliverySchema: "EventGridSchema", |
| 132 | + filter: new EventSubscriptionFilter( |
| 133 | + includedEventTypes: includedEventTypes), |
| 134 | + destination: new WebHookEventSubscriptionDestination(webhookUri)); |
| 135 | + |
| 136 | +await eventGridClient.EventSubscriptions.CreateOrUpdateAsync( |
| 137 | + scope: resourceId, |
| 138 | + eventSubscriptionName: "<eventSubscriptionName>", |
| 139 | + eventSubscriptionInfo: eventSubscription); |
| 140 | +``` |
| 141 | + |
| 142 | +## Update event subscription |
| 143 | +This code sample shows how to update the event subscription to add more events, you want to receive on the webhook subscriber endpoint. |
| 144 | + |
| 145 | +```csharp |
| 146 | +string webhookUri = $"<webhookUri>"; |
| 147 | +string resourceId="/subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Communication/CommunicationServices/<acsResourceName>"; |
| 148 | +string[] additionalEventTypes = new string[]{ |
| 149 | + "Microsoft.Communication.ChatMessageReceived" |
| 150 | + }; |
| 151 | + |
| 152 | +await eventGridClient.EventSubscriptions.UpdateAsync( |
| 153 | + scope: resourceId, |
| 154 | + eventSubscriptionName: "<eventSubscriptionName>", |
| 155 | + eventSubscriptionUpdateParameters: new EventSubscriptionUpdateParameters( |
| 156 | + filter: new EventSubscriptionFilter(includedEventTypes: additionalEventTypes))); |
| 157 | +``` |
| 158 | + |
| 159 | +## Delete event Subscription |
| 160 | +This code sample shows how to delete the event subscription for the webhook subscriber endpoint. |
| 161 | + |
| 162 | +```csharp |
| 163 | +string webhookUri = $"<webhookUri>"; |
| 164 | +string resourceId="/subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Communication/CommunicationServices/<acsResourceName>"; |
| 165 | + |
| 166 | +await eventGridClient.EventSubscriptions.DeleteAsync( |
| 167 | + scope: resourceId, |
| 168 | + eventSubscriptionName: "<eventSubscriptionName>"); |
| 169 | +``` |
| 170 | + |
0 commit comments