Skip to content

Commit 356e397

Browse files
authored
Merge pull request #178334 from rwike77/nodejs2
adding nodejs sample
2 parents 7a7d23f + 690ca4c commit 356e397

File tree

3 files changed

+150
-22
lines changed

3 files changed

+150
-22
lines changed

articles/app-service/scenario-secure-app-access-microsoft-graph-as-app.md

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ manager: CelesteDG
88
ms.service: app-service-web
99
ms.topic: tutorial
1010
ms.workload: identity
11-
ms.date: 01/28/2021
11+
ms.date: 11/02/2021
1212
ms.author: ryanwi
1313
ms.reviewer: stsoneff
1414
ms.custom: azureday1, devx-track-azurepowershell
@@ -116,7 +116,9 @@ In **Overview**, select **Permissions**, and you'll see the added permissions fo
116116

117117
:::image type="content" alt-text="Screenshot that shows the Permissions pane." source="./media/scenario-secure-app-access-microsoft-graph/enterprise-apps-permissions.png":::
118118

119-
## Call Microsoft Graph (.NET)
119+
## Call Microsoft Graph
120+
121+
# [C#](#tab/programming-language-csharp)
120122

121123
The [DefaultAzureCredential](/dotnet/api/azure.identity.defaultazurecredential) class is used to get a token credential for your code to authorize requests to Microsoft Graph. Create an instance of the [DefaultAzureCredential](/dotnet/api/azure.identity.defaultazurecredential) class, which uses the managed identity to fetch tokens and attach them to the service client. The following code example gets the authenticated token credential and uses it to create a service client object, which gets the users in the group.
122124

@@ -126,7 +128,7 @@ To see this code as part of a sample application, see the [sample on GitHub](htt
126128

127129
Install the [Microsoft.Identity.Web.MicrosoftGraph NuGet package](https://www.nuget.org/packages/Microsoft.Identity.Web.MicrosoftGraph) in your project by using the .NET Core command-line interface or the Package Manager Console in Visual Studio.
128130

129-
# [Command line](#tab/command-line)
131+
#### .NET Core command-line
130132

131133
Open a command line, and switch to the directory that contains your project file.
132134

@@ -136,7 +138,7 @@ Run the install commands.
136138
dotnet add package Microsoft.Identity.Web.MicrosoftGraph
137139
```
138140

139-
# [Package Manager](#tab/package-manager)
141+
#### Package Manager Console
140142

141143
Open the project/solution in Visual Studio, and open the console by using the **Tools** > **NuGet Package Manager** > **Package Manager Console** command.
142144

@@ -145,8 +147,6 @@ Run the install commands.
145147
Install-Package Microsoft.Identity.Web.MicrosoftGraph
146148
```
147149

148-
---
149-
150150
### Example
151151

152152
```csharp
@@ -205,6 +205,55 @@ public async Task OnGetAsync()
205205
}
206206
```
207207

208+
# [Node.js](#tab/programming-language-nodejs)
209+
210+
The `DefaultAzureCredential` class from [@azure/identity](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/README.md) package is used to get a token credential for your code to authorize requests to Azure Storage. Create an instance of the `DefaultAzureCredential` class, which uses the managed identity to fetch tokens and attach them to the service client. The following code example gets the authenticated token credential and uses it to create a service client object, which gets the users in the group.
211+
212+
To see this code as part of a sample application, see the [sample on GitHub](https://github.com/Azure-Samples/ms-identity-easyauth-nodejs-storage-graphapi/tree/main/3-WebApp-graphapi-managed-identity).
213+
214+
### Example
215+
216+
```nodejs
217+
const graphHelper = require('../utils/graphHelper');
218+
const { DefaultAzureCredential } = require("@azure/identity");
219+
220+
exports.getUsersPage = async(req, res, next) => {
221+
222+
const defaultAzureCredential = new DefaultAzureCredential();
223+
224+
try {
225+
const tokenResponse = await defaultAzureCredential.getToken("https://graph.microsoft.com/.default");
226+
227+
const graphClient = graphHelper.getAuthenticatedClient(tokenResponse.token);
228+
229+
const users = await graphClient
230+
.api('/users')
231+
.get();
232+
233+
res.render('users', { user: req.session.user, users: users });
234+
} catch (error) {
235+
next(error);
236+
}
237+
}
238+
```
239+
240+
To query Microsoft Graph, the sample uses the [Microsoft Graph JavaScript SDK](https://github.com/microsoftgraph/msgraph-sdk-javascript). The code for this is located in [utils/graphHelper.js](https://github.com/Azure-Samples/ms-identity-easyauth-nodejs-storage-graphapi/blob/main/3-WebApp-graphapi-managed-identity/controllers/graphController.js) of the full sample:
241+
242+
```nodejs
243+
getAuthenticatedClient = (accessToken) => {
244+
// Initialize Graph client
245+
const client = graph.Client.init({
246+
// Use the provided access token to authenticate requests
247+
authProvider: (done) => {
248+
done(null, accessToken);
249+
}
250+
});
251+
252+
return client;
253+
}
254+
```
255+
---
256+
208257
## Clean up resources
209258

210259
If you're finished with this tutorial and no longer need the web app or associated resources, [clean up the resources you created](scenario-secure-app-clean-up-resources.md).

articles/app-service/scenario-secure-app-access-microsoft-graph-as-user.md

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ manager: CelesteDG
88
ms.service: app-service-web
99
ms.topic: tutorial
1010
ms.workload: identity
11-
ms.date: 09/23/2021
11+
ms.date: 11/02/2021
1212
ms.author: ryanwi
1313
ms.reviewer: stsoneff
1414
ms.custom: azureday1
@@ -119,9 +119,12 @@ az rest --method PUT --url '/subscriptions/{SUBSCRIPTION_ID}/resourceGroups/{RES
119119
```
120120
---
121121

122-
## Call Microsoft Graph (.NET)
122+
## Call Microsoft Graph
123123

124-
Your web app now has the required permissions and also adds Microsoft Graph's client ID to the login parameters. Using the [Microsoft.Identity.Web library](https://github.com/AzureAD/microsoft-identity-web/), the web app gets an access token for authentication with Microsoft Graph. In version 1.2.0 and later, the Microsoft.Identity.Web library integrates with and can run alongside the App Service authentication/authorization module. Microsoft.Identity.Web detects that the web app is hosted in App Service and gets the access token from the App Service authentication/authorization module. The access token is then passed along to authenticated requests with the Microsoft Graph API.
124+
Your web app now has the required permissions and also adds Microsoft Graph's client ID to the login parameters.
125+
126+
# [C#](#tab/programming-language-csharp)
127+
Using the [Microsoft.Identity.Web library](https://github.com/AzureAD/microsoft-identity-web/), the web app gets an access token for authentication with Microsoft Graph. In version 1.2.0 and later, the Microsoft.Identity.Web library integrates with and can run alongside the App Service authentication/authorization module. Microsoft.Identity.Web detects that the web app is hosted in App Service and gets the access token from the App Service authentication/authorization module. The access token is then passed along to authenticated requests with the Microsoft Graph API.
125128

126129
To see this code as part of a sample application, see the [sample on GitHub](https://github.com/Azure-Samples/ms-identity-easyauth-dotnet-storage-graphapi/tree/main/2-WebApp-graphapi-on-behalf).
127130

@@ -134,7 +137,7 @@ To see this code as part of a sample application, see the [sample on GitHub](htt
134137

135138
Install the [Microsoft.Identity.Web](https://www.nuget.org/packages/Microsoft.Identity.Web/) and [Microsoft.Identity.Web.MicrosoftGraph](https://www.nuget.org/packages/Microsoft.Identity.Web.MicrosoftGraph) NuGet packages in your project by using the .NET Core command-line interface or the Package Manager Console in Visual Studio.
136139

137-
# [Command line](#tab/command-line)
140+
#### .NET Core command line
138141

139142
Open a command line, and switch to the directory that contains your project file.
140143

@@ -146,7 +149,7 @@ dotnet add package Microsoft.Identity.Web.MicrosoftGraph
146149
dotnet add package Microsoft.Identity.Web
147150
```
148151

149-
# [Package Manager](#tab/package-manager)
152+
#### Package Manager Console
150153

151154
Open the project/solution in Visual Studio, and open the console by using the **Tools** > **NuGet Package Manager** > **Package Manager Console** command.
152155

@@ -157,8 +160,6 @@ Install-Package Microsoft.Identity.Web.MicrosoftGraph
157160
Install-Package Microsoft.Identity.Web
158161
```
159162

160-
---
161-
162163
### Startup.cs
163164

164165
In the *Startup.cs* file, the ```AddMicrosoftIdentityWebApp``` method adds Microsoft.Identity.Web to your web app. The ```AddMicrosoftGraph``` method adds Microsoft Graph support.
@@ -271,6 +272,54 @@ public class IndexModel : PageModel
271272
}
272273
```
273274

275+
# [Node.js](#tab/programming-language-nodejs)
276+
277+
The web app gets the user's access token from the incoming requests header, which is then passed down to Microsoft Graph client to make an authenticated request to the `/me` endpoint.
278+
279+
To see this code as part of a sample application, see *graphController.js* in the [sample on GitHub](https://github.com/Azure-Samples/ms-identity-easyauth-nodejs-storage-graphapi/tree/main/2-WebApp-graphapi-on-behalf).
280+
281+
```nodejs
282+
const graphHelper = require('../utils/graphHelper');
283+
284+
// Some code omitted for brevity.
285+
286+
exports.getProfilePage = async(req, res, next) => {
287+
288+
try {
289+
const graphClient = graphHelper.getAuthenticatedClient(req.session.protectedResources["graphAPI"].accessToken);
290+
291+
const profile = await graphClient
292+
.api('/me')
293+
.get();
294+
295+
res.render('profile', { isAuthenticated: req.session.isAuthenticated, profile: profile, appServiceName: appServiceName });
296+
} catch (error) {
297+
next(error);
298+
}
299+
}
300+
```
301+
302+
To query Microsoft Graph, use the [Microsoft Graph JavaScript SDK](https://github.com/microsoftgraph/msgraph-sdk-javascript). The code for this is located in [utils/graphHelper.js](https://github.com/Azure-Samples/ms-identity-easyauth-nodejs-storage-graphapi/blob/main/2-WebApp-graphapi-on-behalf/utils/graphHelper.js):
303+
304+
```nodejs
305+
const graph = require('@microsoft/microsoft-graph-client');
306+
307+
// Some code omitted for brevity.
308+
309+
getAuthenticatedClient = (accessToken) => {
310+
// Initialize Graph client
311+
const client = graph.Client.init({
312+
// Use the provided access token to authenticate requests
313+
authProvider: (done) => {
314+
done(null, accessToken);
315+
}
316+
});
317+
318+
return client;
319+
}
320+
```
321+
---
322+
274323
## Clean up resources
275324

276325
If you're finished with this tutorial and no longer need the web app or associated resources, [clean up the resources you created](scenario-secure-app-clean-up-resources.md).

articles/app-service/scenario-secure-app-access-storage.md

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ description: In this tutorial, you learn how to access Azure Storage for an app
44
services: storage, app-service-web
55
author: rwike77
66
manager: CelesteDG
7-
87
ms.service: app-service-web
98
ms.topic: tutorial
109
ms.workload: identity
11-
ms.date: 06/16/2021
10+
ms.date: 11/02/2021
1211
ms.author: ryanwi
1312
ms.reviewer: stsoneff
1413
ms.custom: azureday1, devx-track-azurecli, devx-track-azurepowershell, subject-rbac-steps
@@ -203,8 +202,8 @@ az role assignment create --assignee $spID --role 'Storage Blob Data Contributor
203202

204203
---
205204

206-
## Access Blob Storage (.NET)
207-
205+
## Access Blob Storage
206+
# [C#](#tab/programming-language-csharp)
208207
The [DefaultAzureCredential](/dotnet/api/azure.identity.defaultazurecredential) class is used to get a token credential for your code to authorize requests to Azure Storage. Create an instance of the [DefaultAzureCredential](/dotnet/api/azure.identity.defaultazurecredential) class, which uses the managed identity to fetch tokens and attach them to the service client. The following code example gets the authenticated token credential and uses it to create a service client object, which uploads a new blob.
209208

210209
To see this code as part of a sample application, see the [sample on GitHub](https://github.com/Azure-Samples/ms-identity-easyauth-dotnet-storage-graphapi/tree/main/1-WebApp-storage-managed-identity).
@@ -213,7 +212,7 @@ To see this code as part of a sample application, see the [sample on GitHub](htt
213212

214213
Install the [Blob Storage NuGet package](https://www.nuget.org/packages/Azure.Storage.Blobs/) to work with Blob Storage and the [Azure Identity client library for .NET NuGet package](https://www.nuget.org/packages/Azure.Identity/) to authenticate with Azure AD credentials. Install the client libraries by using the .NET Core command-line interface or the Package Manager Console in Visual Studio.
215214

216-
# [Command line](#tab/command-line)
215+
#### .NET Core command-line
217216

218217
Open a command line, and switch to the directory that contains your project file.
219218

@@ -225,8 +224,7 @@ dotnet add package Azure.Storage.Blobs
225224
dotnet add package Azure.Identity
226225
```
227226

228-
# [Package Manager](#tab/package-manager)
229-
227+
#### Package Manager Console
230228
Open the project or solution in Visual Studio, and open the console by using the **Tools** > **NuGet Package Manager** > **Package Manager Console** command.
231229

232230
Run the install commands.
@@ -236,8 +234,6 @@ Install-Package Azure.Storage.Blobs
236234
Install-Package Azure.Identity
237235
```
238236

239-
---
240-
241237
### Example
242238

243239
```csharp
@@ -283,6 +279,40 @@ static public async Task UploadBlob(string accountName, string containerName, st
283279
}
284280
```
285281

282+
# [Node.js](#tab/programming-language-nodejs)
283+
The `DefaultAzureCredential` class from [@azure/identity](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/README.md) package is used to get a token credential for your code to authorize requests to Azure Storage. The `BlobServiceClient` class from [@azure/storage-blob](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/storage/storage-blob) package is used to upload a new blob to storage. Create an instance of the `DefaultAzureCredential` class, which uses the managed identity to fetch tokens and attach them to the blob service client. The following code example gets the authenticated token credential and uses it to create a service client object, which uploads a new blob.
284+
285+
To see this code as part of a sample application, see *StorageHelper.js* in the [sample on GitHub](https://github.com/Azure-Samples/ms-identity-easyauth-nodejs-storage-graphapi/tree/main/1-WebApp-storage-managed-identity).
286+
287+
### Example
288+
289+
```nodejs
290+
const { DefaultAzureCredential } = require("@azure/identity");
291+
const { BlobServiceClient } = require("@azure/storage-blob");
292+
const defaultAzureCredential = new DefaultAzureCredential();
293+
294+
// Some code omitted for brevity.
295+
296+
async function uploadBlob(accountName, containerName, blobName, blobContents) {
297+
const blobServiceClient = new BlobServiceClient(
298+
`https://${accountName}.blob.core.windows.net`,
299+
defaultAzureCredential
300+
);
301+
302+
const containerClient = blobServiceClient.getContainerClient(containerName);
303+
304+
try {
305+
await containerClient.createIfNotExists();
306+
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
307+
const uploadBlobResponse = await blockBlobClient.upload(blobContents, blobContents.length);
308+
console.log(`Upload block blob ${blobName} successfully`, uploadBlobResponse.requestId);
309+
} catch (error) {
310+
console.log(error);
311+
}
312+
}
313+
```
314+
---
315+
286316
## Clean up resources
287317

288318
If you're finished with this tutorial and no longer need the web app or associated resources, [clean up the resources you created](scenario-secure-app-clean-up-resources.md).

0 commit comments

Comments
 (0)