Skip to content

Commit 1e0d3bb

Browse files
authored
Merge pull request #205166 from AikoBB/feature/acs-identity/cte-ga-link-to-quickstart
Final code section for CTE .NET&JS
2 parents 66f6a31 + c755728 commit 1e0d3bb

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

articles/communication-services/quickstarts/includes/manage-teams-identity-js.md

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ ms.author: gistefan
1717

1818
- [Node.js](https://nodejs.org/) Active LTS and Maintenance LTS versions (8.11.1 and 10.14.1 recommended).
1919

20+
## Final code
21+
Find the finalized code for this quickstart on [GitHub](https://github.com/Azure-Samples/communication-services-javascript-quickstarts/tree/main/manage-teams-identity-mobile-and-desktop).
22+
2023
## Set up
2124

2225
### Create a new Node.js Application
@@ -42,6 +45,7 @@ Use the `npm install` command to install the Azure Communication Services Identi
4245
npm install @azure/communication-identity@latest --save
4346
npm install @azure/msal-node --save
4447
npm install express --save
48+
npm install dotenv --save
4549

4650
```
4751

@@ -59,10 +63,13 @@ From the project directory:
5963
const { CommunicationIdentityClient } = require('@azure/communication-identity');
6064
const { PublicClientApplication, CryptoProvider } = require('@azure/msal-node');
6165
const express = require("express");
62-
66+
67+
// You will need to set environment variables in .env
6368
const SERVER_PORT = process.env.PORT || 80;
6469
const REDIRECT_URI = `http://localhost:${SERVER_PORT}/redirect`;
65-
70+
const clientId = process.env['AAD_CLIENT_ID'];
71+
const tenantId = process.env['AAD_TENANT_ID'];
72+
6673
// Quickstart code goes here
6774

6875
app.listen(SERVER_PORT, () => console.log(`Communication access token application started on ${SERVER_PORT}!`))
@@ -76,54 +83,53 @@ From the project directory:
7683
The first step in the token exchange flow is getting a token for your Teams user by using [Microsoft.Identity.Client](../../../active-directory/develop/reference-v2-libraries.md).
7784

7885
```javascript
79-
80-
const clientId = "<contoso_application_id>";
81-
const tenantId = "<contoso_tenant_id>";
86+
// Create configuration object that will be passed to MSAL instance on creation.
8287
const msalConfig = {
8388
auth: {
8489
clientId: clientId,
8590
authority: `https://login.microsoftonline.com/${tenantId}`,
8691
}
8792
};
8893

94+
// Create an instance of PublicClientApplication
8995
const pca = new PublicClientApplication(msalConfig);
9096
const provider = new CryptoProvider();
9197

9298
const app = express();
9399

94100
let pkceVerifier = "";
101+
const scopes = [
102+
"https://auth.msft.communication.azure.com/Teams.ManageCalls",
103+
"https://auth.msft.communication.azure.com/Teams.ManageChats"
104+
];
95105

96106
app.get('/', async (req, res) => {
107+
// Generate PKCE Codes before starting the authorization flow
97108
const {verifier, challenge} = await provider.generatePkceCodes();
98109
pkceVerifier = verifier;
99110

100111
const authCodeUrlParameters = {
101-
scopes: [
102-
"https://auth.msft.communication.azure.com/Teams.ManageCalls",
103-
"https://auth.msft.communication.azure.com/Teams.ManageChats"
104-
],
112+
scopes: scopes,
105113
redirectUri: REDIRECT_URI,
106114
codeChallenge: challenge,
107115
codeChallengeMethod: "S256"
108116
};
109-
117+
// Get url to sign user in and consent to scopes needed for application
110118
pca.getAuthCodeUrl(authCodeUrlParameters).then((response) => {
111119
res.redirect(response);
112120
}).catch((error) => console.log(JSON.stringify(error)));
113121
});
114122

115123
app.get('/redirect', async (req, res) => {
124+
// Create request parameters object for acquiring the AAD token and object ID of a Teams user
116125
const tokenRequest = {
117126
code: req.query.code,
118-
scopes: [
119-
"https://auth.msft.communication.azure.com/Teams.ManageCalls",
120-
"https://auth.msft.communication.azure.com/Teams.ManageChats"
121-
],
127+
scopes: scopes,
122128
redirectUri: REDIRECT_URI,
123129
codeVerifier: pkceVerifier,
124130
};
125-
126-
pca.acquireTokenByCode(tokenRequest).then((response) => {
131+
// Retrieve the AAD token and object ID of a Teams user
132+
pca.acquireTokenByCode(tokenRequest).then(async(response) => {
127133
console.log("Response:", response);
128134
let teamsUserAadToken = response.accessToken;
129135
let userObjectId = response.uniqueId;
@@ -156,12 +162,12 @@ const identityClient = new CommunicationIdentityClient(connectionString);
156162
Use the `getTokenForTeamsUser` method to issue an access token for the Teams user that can be used with the Azure Communication Services SDKs.
157163

158164
```javascript
159-
let teamsToken = response.accessToken;
165+
//Exchange the Azure AD access token of the Teams User for a Communication Identity access token
160166
let accessToken = await identityClient.getTokenForTeamsUser({
161167
teamsUserAadToken: teamsUserAadToken,
162168
clientId: clientId,
163169
userObjectId: userObjectId,
164-
});;
170+
});
165171
console.log("Token:", accessToken);
166172
```
167173

articles/communication-services/quickstarts/includes/manage-teams-identity-net.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ ms.author: gistefan
1717

1818
- The latest version [.NET SDK](https://dotnet.microsoft.com/download/dotnet) for your operating system.
1919

20+
## Final code
21+
Find the finalized code for this quickstart on [GitHub](https://github.com/Azure-Samples/communication-services-dotnet-quickstarts/tree/main/ManageTeamsIdentityMobileAndDesktop).
22+
2023
## Set up
2124

2225
### Create a new C# application
@@ -76,25 +79,29 @@ namespace CommunicationAccessTokensQuickstart
7679

7780
### Step 1: Receive the Azure AD user token and object ID via the MSAL library
7881

79-
The first step in the token exchange flow is getting a token for your Teams user by using [Microsoft.Identity.Client](../../../active-directory/develop/reference-v2-libraries.md).
82+
The first step in the token exchange flow is getting a token for your Teams user by using [Microsoft.Identity.Client](../../../active-directory/develop/reference-v2-libraries.md). The code below retrieves Azure AD client ID and tenant ID from environment variables named `AAD_CLIENT_ID` and `AAD_TENANT_ID`.
8083

8184
```csharp
82-
string appId = "<contoso_application_id>";
83-
string tenantId = "<contoso_tenant_id>";
85+
// This code demonstrates how to fetch an AAD client ID and tenant ID
86+
// from an environment variable.
87+
string appId = Environment.GetEnvironmentVariable("AAD_CLIENT_ID");
88+
string tenantId = Environment.GetEnvironmentVariable("AAD_TENANT_ID");
8489
string authority = $"https://login.microsoftonline.com/{tenantId}";
8590
string redirectUri = "http://localhost";
8691

92+
// Create an instance of PublicClientApplication
8793
var aadClient = PublicClientApplicationBuilder
8894
.Create(appId)
8995
.WithAuthority(authority)
9096
.WithRedirectUri(redirectUri)
9197
.Build();
9298

93-
List<string> scopes = new List<string> {
99+
List<string> scopes = new() {
94100
"https://auth.msft.communication.azure.com/Teams.ManageCalls",
95101
"https://auth.msft.communication.azure.com/Teams.ManageChats"
96102
};
97103

104+
// Retrieve the AAD token and object ID of a Teams user
98105
var result = await aadClient
99106
.AcquireTokenInteractive(scopes)
100107
.ExecuteAsync();

0 commit comments

Comments
 (0)