Skip to content

Commit 063fda1

Browse files
committed
added sample app for dotnet
1 parent 16609ee commit 063fda1

File tree

3 files changed

+145
-1
lines changed

3 files changed

+145
-1
lines changed

articles/communication-services/quickstarts/identity/entra-id-authentication-integration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ The developer's required actions are shown in following diagram:
178178
> [!NOTE]
179179
> The following sections describe how to create `AzureCommunicationTokenCredential`.
180180
181+
::: zone pivot="programming-language-csharp"
182+
[!INCLUDE [.NET](./includes/entra-id/support-entra-id-users-net.md)]
183+
::: zone-end
184+
181185
::: zone pivot="programming-language-javascript"
182186
[!INCLUDE [JavaScript](./includes/entra-id/support-entra-id-users-js.md)]
183187
::: zone-end

articles/communication-services/quickstarts/identity/includes/entra-id/support-entra-id-users-js.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ app.get('/', async (req, res) => {
125125
}
126126
});
127127
```
128-
Providing scopes is optional. When not specified, the .default scope is automatically used, requesting all API permissions for Communication Services Clients.
128+
Providing scopes is optional. When not specified, the `https://communication.azure.com/clients/.default` scope is automatically used, requesting all API permissions for Communication Services Clients.
129129

130130
<a name='step-3-obtain-acs-access-token-of-the-entra-id-user'></a>
131131

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
---
2+
title: include file
3+
description: include file
4+
services: azure-communication-services
5+
author: aigerimb
6+
manager: soricos
7+
8+
ms.service: azure-communication-services
9+
ms.subservice: azure-communication-services
10+
ms.date: 05/06/2025
11+
ms.topic: include
12+
ms.custom: include file
13+
ms.author: aigerimb
14+
---
15+
16+
## Set up prerequisites
17+
18+
- The latest version [.NET SDK](https://dotnet.microsoft.com/download/dotnet) for your operating system.
19+
- [Azure Identify SDK for .Net](https://www.nuget.org/packages/Azure.Identity) to authenticate with Microsoft Entra ID.
20+
- [Azure Communication Services Common SDK for .Net](https://www.nuget.org/packages/Azure.Communication.Common/) to obtain Azure Communication Services access tokens for Microsoft Entra ID user.
21+
22+
## Final code
23+
Find the finalized code for this quickstart on [GitHub](https://github.com/Azure-Samples/communication-services-dotnet-quickstarts/tree/main/EntraIdUsersSupportQuickstart).
24+
25+
## Set up
26+
27+
### Create a new C# application
28+
29+
In a console window (such as cmd, PowerShell, or Bash), use the `dotnet new` command to create a new console app with the name `EntraIdUsersSupportQuickstart`. This command creates a simple "Hello World" C# project with a single source file: **Program.cs**.
30+
31+
```console
32+
dotnet new console -o EntraIdUsersSupportQuickstart
33+
```
34+
35+
Change your directory to the newly created app folder and use the `dotnet build` command to compile your application.
36+
37+
```console
38+
cd EntraIdUsersSupportQuickstart
39+
dotnet build
40+
```
41+
42+
### Install the package
43+
44+
While still in the application directory, install the Azure Identity and Azure Communication Services Common library for .NET package by using the `dotnet add package` command. The Azure Communication Services Common SDK version should be `1.4.0` or later.
45+
46+
```console
47+
dotnet add package Azure.Identity
48+
dotnet add package Azure.Communication.Common
49+
```
50+
51+
### Set up the app framework
52+
53+
From the project directory:
54+
55+
1. Open **Program.cs** file in a text editor
56+
1. Replace the contents of **Program.cs** with the following code:
57+
58+
```csharp
59+
using Azure.Communication;
60+
using Azure.Identity;
61+
62+
namespace EntraIdUsersSupportQuickstart
63+
{
64+
class Program
65+
{
66+
static async Task Main(string[] args)
67+
{
68+
Console.WriteLine("Azure Communication Services - Obtain Access Token for Entra ID User Quickstart");
69+
70+
// Quickstart code goes here
71+
}
72+
}
73+
}
74+
```
75+
76+
<a name='step-1-obtain-entra-user-token-via-the-identity-library'></a>
77+
78+
### Step 1: Initialize implementation of TokenCredential from Azure Identity SDK
79+
80+
The first step in obtaining Communication Services access token for Entra ID user is getting an Entra ID access token for your Entra ID user by using [Azure.Identity](/dotnet/api/overview/azure/identity-readme?view=azure-dotnet) SDK. The code below retrieves Microsoft Entra client ID and tenant ID from environment variables named `ENTRA_CLIENT_ID` and `ENTRA_TENANT_ID`.
81+
82+
```csharp
83+
// This code demonstrates how to fetch your Microsoft Entra client ID and tenant ID from environment variables.
84+
string clientId = Environment.GetEnvironmentVariable("ENTRA_CLIENT_ID");
85+
string tenantId = Environment.GetEnvironmentVariable("ENTRA_TENANT_ID");
86+
87+
//Initialize InteractiveBrowserCredential for use with CommunicationTokenCredential.
88+
var options = new InteractiveBrowserCredentialOptions
89+
{
90+
TenantId = tenantId,
91+
ClientId = clientId,
92+
};
93+
var entraTokenCredential = new InteractiveBrowserCredential(options);
94+
95+
```
96+
97+
### Step 2: Initialize CommunicationTokenCredential
98+
99+
Instantiate a `CommunicationTokenCredential` with the TokenCredential created above and your Communication Services resource endpoint URI. The code below retrieves the endpoint for the resource from an environment variable named `COMMUNICATION_SERVICES_RESOURCE_ENDPOINT`.
100+
101+
Add the following code to the `Main` method:
102+
103+
```csharp
104+
// This code demonstrates how to fetch your Azure Communication Services resource endpoint URI
105+
// from an environment variable.
106+
string resourceEndpoint = Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_RESOURCE_ENDPOINT");
107+
108+
// Set up CommunicationTokenCredential to request a Communication Services access token for a Microsoft Entra ID user.
109+
var entraTokenCredentialOptions = new EntraCommunicationTokenCredentialOptions(
110+
resourceEndpoint: resourceEndpoint,
111+
entraTokenCredential: entraTokenCredential)
112+
{
113+
Scopes = new[] { "https://communication.azure.com/clients/VoIP" }
114+
};
115+
116+
var credential = new CommunicationTokenCredential(entraTokenCredentialOptions);
117+
118+
```
119+
120+
Providing scopes is optional. When not specified, the `https://communication.azure.com/clients/.default` scope is automatically used, requesting all API permissions for Communication Services Clients.
121+
122+
<a name='step-3-obtain-acs-access-token-of-the-entra-id-user'></a>
123+
124+
### Step 3: Obtain Azure Communication Services access token for Microsoft Entra ID user
125+
126+
Use the `GetTokenAsync` method to obtain an access token for the Entra ID user. The `CommunicationTokenCredential` can be used with the Azure Communication Services SDKs.
127+
128+
```csharp
129+
// To obtain a Communication Services access token for Microsoft Entra ID call GetTokenAsync() method.
130+
var accessToken = await credential.GetTokenAsync();
131+
Console.WriteLine($"Token: {accessToken.Token}");
132+
```
133+
134+
## Run the code
135+
136+
Run the application from your application directory with the `dotnet run` command.
137+
138+
```console
139+
dotnet run
140+
```

0 commit comments

Comments
 (0)