Skip to content

Commit d2a90b3

Browse files
committed
nodejs samples
1 parent c997a5f commit d2a90b3

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

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

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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,48 @@ public class IndexModel : PageModel
271272
}
272273
```
273274

275+
# [Node.js](#tab/programming-language-nodejs)
276+
277+
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/2-WebApp-graphapi-on-behalf).
278+
279+
The sample 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:
280+
281+
```nodejs
282+
const graphHelper = require('../utils/graphHelper');
283+
284+
exports.getProfilePage = async(req, res, next) => {
285+
286+
try {
287+
const graphClient = graphHelper.getAuthenticatedClient(req.session.protectedResources["graphAPI"].accessToken);
288+
289+
const profile = await graphClient
290+
.api('/me')
291+
.get();
292+
293+
res.render('profile', { isAuthenticated: req.session.isAuthenticated, profile: profile, appServiceName: appServiceName });
294+
} catch (error) {
295+
next(error);
296+
}
297+
}
298+
```
299+
300+
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:
301+
302+
```nodejs
303+
getAuthenticatedClient = (accessToken) => {
304+
// Initialize Graph client
305+
const client = graph.Client.init({
306+
// Use the provided access token to authenticate requests
307+
authProvider: (done) => {
308+
done(null, accessToken);
309+
}
310+
});
311+
312+
return client;
313+
}
314+
```
315+
---
316+
274317
## Clean up resources
275318

276319
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: 33 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: 10/27/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,34 @@ 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 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+
async function uploadBlob(accountName, containerName, blobName, blobContents) {
291+
const blobServiceClient = new BlobServiceClient(
292+
`https://${accountName}.blob.core.windows.net`,
293+
defaultAzureCredential
294+
);
295+
296+
const containerClient = blobServiceClient.getContainerClient(containerName);
297+
298+
try {
299+
await containerClient.createIfNotExists();
300+
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
301+
const uploadBlobResponse = await blockBlobClient.upload(blobContents, blobContents.length);
302+
console.log(`Upload block blob ${blobName} successfully`, uploadBlobResponse.requestId);
303+
} catch (error) {
304+
console.log(error);
305+
}
306+
}
307+
```
308+
---
309+
286310
## Clean up resources
287311

288312
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)