diff --git a/articles/communication-services/quickstarts/events/includes/create-event-subscription-net.md b/articles/communication-services/quickstarts/events/includes/create-event-subscription-net.md deleted file mode 100644 index 0ec1db3f39f78..0000000000000 --- a/articles/communication-services/quickstarts/events/includes/create-event-subscription-net.md +++ /dev/null @@ -1,175 +0,0 @@ ---- -author: pgrandhi -ms.service: azure-communication-services -ms.topic: include -ms.date: 06/28/2025 -ms.author: pgrandhi ---- - -## Prerequisites - -- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/dotnet/). -- The latest version [.NET Core SDK](https://dotnet.microsoft.com/download/dotnet-core) for your operating system. -- The latest version of the [.NET Microsoft Azure Event Grid Management SDK](/azure/event-grid/sdk-overview). -- The latest version of the [Azure Identity](/dotnet/api/overview/azure/identity-readme) library. -- An [Azure Communication Services resource](../../create-communication-resource.md). - -[!INCLUDE [register-event-grid-resource-provider.md](register-event-grid-resource-provider.md)] - -## Install the SDK - -First, install the Microsoft Azure Event Grid Management library for .NET with [NuGet](https://www.nuget.org/): - -```csharp -dotnet add package Azure.ResourceManager.EventGrid; -``` - -Include the Event Grid Management SDK in your C# project: - -```csharp -using Microsoft.Azure.Management.EventGrid; -using Microsoft.Azure.Management.EventGrid.Models; -``` - -## Authenticate with Azure Identity library - -1. Create a Microsoft Entra application and Service Principal and set up a client secret or trusted certificate issued by certificate authority. Follow the instructions at [Register a Microsoft Entra app and create a service principal](/entra/identity-platform/howto-create-service-principal-portal). - -1. Store the secret or the certificate in the Azure Keyvault. - -1. Give contributor or owner access to the subscription to that application following the instructions at [Grant a user access to Azure resources using the Azure portal](/azure/role-based-access-control/quickstart-assign-role-user-portal). - -1. Read more about [Authorizing access to Event Grid resources](/azure/event-grid/security-authorization). - -The Azure Identity library provides Microsoft Entra ID token authentication support across the Azure SDK. It provides a set of TokenCredential implementations, which you can use to construct Azure SDK clients that support Microsoft Entra token authentication. For more information, see [Azure Identity client library for .NET](/dotnet/api/overview/azure/identity-readme). - -1. Include the Azure Identity client library for .NET with [NuGet](https://www.nuget.org/). - - ```csharp - dotnet add package Azure.Identity; - dotnet add package Azure.Security.KeyVault.Secrets - ``` - -2. Include the Azure Identity library in your C# project. - - ```csharp - using Microsoft.Azure.Identity; - using Azure.Security.KeyVault.Secrets - ``` - -3. You can either pass the secret credentials or certificate credentials to get access token, based on how your Service Principal is configured. - - - Get access token using secret credential - - 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). - - ```csharp - // Authenticate the Keyvault client with DefaultAzureCredential and get the secret. - SecretClient secretClient = new SecretClient(new Uri("https://myvault.vault.azure.net/"), new DefaultAzureCredential()); - string clientSecret = await secretClient.GetSecretAsync(secretName).Value; - - // Get access token using secret credentials - string[] scopes = { "https://management.azure.com/.default" }; - var application = ConfidentialClientApplicationBuilder - .Create('your-servicePrincipal-appId') - .WithAuthority(authorityUri: new Uri(authority), validateAuthority: true) - .WithTenantId('your-tenant_id') - .WithClientSecret(clientSecret) - .Build(); - - var token = await application - .AcquireTokenForClient(scopes) - .ExecuteAsync(); - ``` - - - Get access token using certificate credential. - - 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). - - Read more about the Microsoft Entra application configuration authority in [Application configuration options](/entra/identity-platform/msal-client-application-configuration). - - ```csharp - // Authenticate the certificate client with DefaultAzureCredential and get the certificate. - CertificateClient certificateClient = new SecretClient(new Uri("https://myvault.vault.azure.net/"), new DefaultAzureCredential()); - X509Certificat2 cert = await certificateClient.DownloadCertificateAsync(certificateName); - - // Get access token using certificate credentials - string[] scopes = { "https://management.azure.com/.default" }; - string authority = "https://login.microsoftonline.com//"; - var application = ConfidentialClientApplicationBuilder - .Create('') - .WithAuthority(authorityUri: new Uri(authority), validateAuthority: true) - .WithTenantId("") - .WithCertificate(cert) - .Build(); - - var token = await application - .AcquireTokenForClient(scopes) - .WithSendX5C(true) - .ExecuteAsync(); - ``` - -4. Authenticate `EventGridManagementClient` with access token using secret or certificate credentials. - -```csharp -// Authenticate EventGridManagementClient with Microsoft Entra ID access token credential -eventGridClient = new EventGridManagementClient(new Uri("https://management.azure.com/"), - new TokenCredentials(token.AccessToken)); - -eventGridClient.SubscriptionId = 'your_subscription_id'; -``` - -## Create Event Subscription - -This code sample shows how to create the event subscription for the webhook subscriber endpoint. - -```csharp -string webhookUri = $""; -string resourceId="/subscriptions//resourceGroups//providers/Microsoft.Communication/CommunicationServices/"; -string[] includedEventTypes = new string[]{ "Microsoft.Communication.SMSReceived", - "Microsoft.Communication.SMSDeliveryReportReceived" - }; - -EventSubscription eventSubscription = new EventSubscription( - name: "", - eventDeliverySchema: "EventGridSchema", - filter: new EventSubscriptionFilter( - includedEventTypes: includedEventTypes), - destination: new WebHookEventSubscriptionDestination(webhookUri)); - -await eventGridClient.EventSubscriptions.CreateOrUpdateAsync( - scope: resourceId, - eventSubscriptionName: "", - eventSubscriptionInfo: eventSubscription); -``` - -## Update event subscription - -This code sample shows how to update the event subscription to add more events that you want to receive on the webhook subscriber endpoint. - -```csharp -string webhookUri = $""; -string resourceId="/subscriptions//resourceGroups//providers/Microsoft.Communication/CommunicationServices/"; -string[] additionalEventTypes = new string[]{ - "Microsoft.Communication.ChatMessageReceived" - }; - -await eventGridClient.EventSubscriptions.UpdateAsync( - scope: resourceId, - eventSubscriptionName: "", - eventSubscriptionUpdateParameters: new EventSubscriptionUpdateParameters( - filter: new EventSubscriptionFilter(includedEventTypes: additionalEventTypes))); -``` - -## Delete event Subscription - -This sample code shows how to delete the event subscription for the webhook subscriber endpoint. - -```csharp -string webhookUri = $""; -string resourceId="/subscriptions//resourceGroups//providers/Microsoft.Communication/CommunicationServices/"; - -await eventGridClient.EventSubscriptions.DeleteAsync( - scope: resourceId, - eventSubscriptionName: ""); -``` diff --git a/articles/communication-services/quickstarts/events/subscribe-to-events.md b/articles/communication-services/quickstarts/events/subscribe-to-events.md index d9d0b866fbb22..9c2ffd641ced7 100644 --- a/articles/communication-services/quickstarts/events/subscribe-to-events.md +++ b/articles/communication-services/quickstarts/events/subscribe-to-events.md @@ -2,10 +2,10 @@ title: Subscribe to events titleSuffix: An Azure Communication Services article description: This article describes how to subscribe to events from Azure Communication Services. -author: pgrandhi +author: awang119 manager: rasubram services: azure-communication-services -ms.author: pgrandhi +ms.author: anniewang ms.date: 06/28/2025 ms.topic: quickstart ms.service: azure-communication-services @@ -31,10 +31,6 @@ This article describes the process of setting up a webhook as a subscriber for S [!INCLUDE [Azure CLI](./includes/create-event-subscription-az-cli.md)] ::: zone-end -::: zone pivot="platform-net" -[!INCLUDE [.NET](./includes/create-event-subscription-net.md)] -::: zone-end - ::: zone pivot="platform-powershell" [!INCLUDE [PowerShell](./includes/create-event-subscription-powershell.md)] ::: zone-end