Skip to content

Commit c69825a

Browse files
committed
adding nodejs sample
1 parent d059665 commit c69825a

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

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

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +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
120120

121+
# [C#](#tab/programming-language-csharp)
121122
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.
122123

123124
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/3-WebApp-graphapi-managed-identity).
@@ -126,7 +127,7 @@ To see this code as part of a sample application, see the [sample on GitHub](htt
126127

127128
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.
128129

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

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

@@ -136,7 +137,7 @@ Run the install commands.
136137
dotnet add package Microsoft.Identity.Web.MicrosoftGraph
137138
```
138139

139-
# [Package Manager](#tab/package-manager)
140+
#### Package Manager Console
140141

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

@@ -145,8 +146,6 @@ Run the install commands.
145146
Install-Package Microsoft.Identity.Web.MicrosoftGraph
146147
```
147148

148-
---
149-
150149
### Example
151150

152151
```csharp
@@ -204,6 +203,55 @@ public async Task OnGetAsync()
204203
Users = msGraphUsers;
205204
}
206205
```
206+
---
207+
208+
# [Node.js](#tab/programming-language-nodejs)
209+
The call to Microsoft Graph is performed in the controllers/graphController.js file getUsersPage controller. 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.
210+
211+
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).
212+
213+
### Example
214+
215+
```nodejs
216+
const graphHelper = require('../utils/graphHelper');
217+
const { DefaultAzureCredential } = require("@azure/identity");
218+
219+
exports.getUsersPage = async(req, res, next) => {
220+
221+
const defaultAzureCredential = new DefaultAzureCredential();
222+
223+
try {
224+
const tokenResponse = await defaultAzureCredential.getToken("https://graph.microsoft.com/.default");
225+
226+
const graphClient = graphHelper.getAuthenticatedClient(tokenResponse.token);
227+
228+
const users = await graphClient
229+
.api('/users')
230+
.get();
231+
232+
res.render('users', { user: req.session.user, users: users });
233+
} catch (error) {
234+
next(error);
235+
}
236+
}
237+
```
238+
239+
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:
240+
241+
```nodejs
242+
getAuthenticatedClient = (accessToken) => {
243+
// Initialize Graph client
244+
const client = graph.Client.init({
245+
// Use the provided access token to authenticate requests
246+
authProvider: (done) => {
247+
done(null, accessToken);
248+
}
249+
});
250+
251+
return client;
252+
}
253+
```
254+
---
207255

208256
## Clean up resources
209257

0 commit comments

Comments
 (0)