You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/communication-services/quickstarts/identity/includes/active-directory/service-principal-net.md
+38-21Lines changed: 38 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,34 +1,44 @@
1
1
> [!NOTE]
2
2
> Find the finalized code for this quickstart on [GitHub](https://github.com/Azure-Samples/communication-services-dotnet-quickstarts/tree/main/use-managed-Identity)
3
3
4
-
## Setting up
4
+
## Overview
5
5
6
-
### Create a new C# application
6
+
This quickstart demonstrates how to use managed identities via Azure Service Principals to authenticate with Azure Communication Services. It provides examples for issuing an access token for Voice over IP (VoIP) calls and sending SMS messages.
7
7
8
-
In a console window (such as cmd, PowerShell, or Bash), use the `dotnet new` command to create a new console app with the name `ActiveDirectoryQuickstart`. This command creates a simple "Hello World" C# project with a single source file: `Program.cs`.
8
+
## Setting Up
9
+
10
+
### Create a New C# Application
11
+
12
+
The goal is to create a new console application in C# to run the quickstart code. Open a terminal window (e.g., Command Prompt, PowerShell, or Bash) and execute the following command to create a new console app named `ActiveDirectoryAuthenticationQuickstart`:
9
13
10
14
```console
11
15
dotnet new console -o ActiveDirectoryAuthenticationQuickstart
12
16
```
13
17
14
-
Change your directory to the newly created app folder and use the `dotnet build` command to compile your application.
18
+
This command will generate a simple "Hello World" C# project, including a single source file: `Program.cs`.
19
+
20
+
### Build the Application
21
+
22
+
Navigate to the newly created app folder and compile your application using the `dotnet build` command:
15
23
16
24
```console
17
25
cd ActiveDirectoryAuthenticationQuickstart
18
26
dotnet build
19
27
```
20
28
21
-
### Install the SDK packages
29
+
### Install the Required SDK Packages
30
+
31
+
To interact with Azure Communication Services and Azure Identity, add the following NuGet packages to your project:
22
32
23
33
```console
24
34
dotnet add package Azure.Communication.Identity
25
35
dotnet add package Azure.Communication.Sms
26
36
dotnet add package Azure.Identity
27
37
```
28
38
29
-
### Use the SDK packages
39
+
### Update the Program.cs File
30
40
31
-
Add the following `using` directives to `Program.cs` to use the Azure Identity and Azure Storage SDKs.
41
+
To use the installed Azure SDK packages, include the following `using` directives at the top of your `Program.cs` file:
32
42
33
43
```csharp
34
44
usingAzure.Identity;
@@ -38,17 +48,17 @@ using Azure.Core;
38
48
usingAzure;
39
49
```
40
50
41
-
## Create a DefaultAzureCredential
51
+
## Authenticate with DefaultAzureCredential
42
52
43
-
We'll be using the [DefaultAzureCredential](/dotnet/api/azure.identity.defaultazurecredential) for this quickstart. This credential is suitable for production and development environments. As it is needed for each operation let's create it within the `Program.cs` class. Add the following to the top of the file.
53
+
For this quickstart, we'll use the [DefaultAzureCredential](/dotnet/api/azure.identity.defaultazurecredential), which is suitable for both development and production environments. Declare an instance of this credential at the class level in `Program.cs`:
@@ -60,9 +70,9 @@ public AccessToken CreateIdentityAndGetTokenAsync(Uri resourceEndpoint)
60
70
}
61
71
```
62
72
63
-
## Send an SMS with service principals
73
+
## Send an SMS with Service Principals
64
74
65
-
As another example of using service principals, we'll add this code which uses the same credential to send an SMS:
75
+
To demonstrate sending an SMS, add the following method to your `Program.cs` file. This method uses the Azure Communication Services SDK to send an SMS message:
@@ -79,9 +89,9 @@ public SmsSendResult SendSms(Uri resourceEndpoint, string from, string to, strin
79
89
}
80
90
```
81
91
82
-
## Write the Main method
92
+
## Write the Main Method
83
93
84
-
Your `Program.cs`should already have a Main method, let's add some code which will call our previously created code to demonstrate the use of service principals:
94
+
In the `Main` method of your `Program.cs`file, add code to call the methods you created for issuing a token and sending an SMS. Your `Main` method should look similar to this:
85
95
86
96
```csharp
87
97
staticvoidMain(string[] args)
@@ -156,11 +166,18 @@ class Program
156
166
157
167
## Run the program
158
168
159
-
You should now be able to run your application, using `dotnet run` from your application folder. The output should resemble the following:
169
+
It is time to run your application and verify that it retrieves an access token and sends an SMS. Open a terminal, navigate to your application directory, and run:
170
+
171
+
```console
172
+
dotnet run
160
173
```
161
-
Retrieving new Access Token, using Service Principals
162
-
Retrieved Access Token: ey....
163
-
Sending SMS using Service Principals
164
-
Sms id: ...
165
-
Send Result Successful: True
174
+
175
+
The console output should appear as follows:
176
+
177
+
```Bash
178
+
Retrieving new Access Token, using Service Principals
0 commit comments