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/storage/common/multiple-identity-scenarios.md
+116-2Lines changed: 116 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -228,9 +228,45 @@ public class ExampleService {
228
228
}
229
229
```
230
230
231
+
#### [JavaScript](#tab/javascript)
232
+
233
+
1. Inside of your project, use [npm](https://docs.npmjs.com/) to add a reference to the `@azure/identity` package. This library contains all of the necessary entities to implement `DefaultAzureCredential`. Install any other [Azure SDK libraries](https://www.npmjs.com/search?q=%40azure) which are relevant to your app.
2. At the top of your `index.js` file, add the following `import` statements to import the necessary client classes for the services your app will connect to:
240
+
241
+
```javascript
242
+
import { DefaultAzureCredential } from "@azure/identity";
243
+
import { BlobServiceClient } from "@azure/storage-blob";
244
+
import { KeyClient } from "@azure/keyvault-keys";
245
+
```
246
+
247
+
3. Within the `index.js` file, create client objects for the Azure services your app will connect to. The following examples connect to Blob Storage and Key Vault using the corresponding SDK classes.
// Create client for Key Vault using managed identity
261
+
const keyClient = new KeyClient(`https://${keyVaultName}.vault.azure.net`, new DefaultAzureCredential());
262
+
263
+
// Create a new key in Key Vault
264
+
const result = await keyClient.createKey(keyVaultName, "RSA");
265
+
```
266
+
231
267
---
232
268
233
-
When this application code runs locally, `DefaultAzureCredential` will search down a credential chain for the first available credentials. If the `Managed_Identity_Client_ID` is null locally, it will automatically use the credentials from your local Azure CLI or Visual Studio sign-in. You can read more about this process in the [Azure Identity library overview](/dotnet/api/overview/azure/Identity-readme#defaultazurecredential).
269
+
When this application code runs locally, `DefaultAzureCredential` will search a credential chain forthe first available credentials. If the `Managed_Identity_Client_ID` is null locally, it will automatically use the credentials from your local Azure CLI or Visual Studio sign-in. You can read more about this processin the [Azure Identity library overview](/dotnet/api/overview/azure/Identity-readme#defaultazurecredential).
234
270
235
271
When the application is deployed to Azure, `DefaultAzureCredential` will automatically retrieve the `Managed_Identity_Client_ID` variable from the app service environment. That value becomes available when a managed identity is associated with your app.
236
272
@@ -251,7 +287,7 @@ To configure this setup in your code, make sure your application registers separ
251
287
252
288
```csharp
253
289
// Get the first user-assigned managed identity ID to connect to shared storage
// First blob storage client that using a managed identity
257
293
BlobServiceClient blobServiceClient = new BlobServiceClient(
@@ -475,6 +511,84 @@ public class ExampleService {
475
511
}
476
512
```
477
513
514
+
#### [JavaScript](#tab/javascript)
515
+
516
+
1. Inside of your project, use [npm](https://docs.npmjs.com/) to add a reference to the `@azure/identity` package. This library contains all of the necessary entities to implement `DefaultAzureCredential`. Install any other [Azure SDK libraries](https://www.npmjs.com/search?q=%40azure) which are relevant to your app.
2. At the top of your `index.js` file, add the following `import` statements to import the necessary client classes for the services your app will connect to:
523
+
524
+
```javascript
525
+
import { DefaultAzureCredential } from "@azure/identity";
526
+
import { BlobServiceClient } from "@azure/storage-blob";
527
+
import { KeyClient } from "@azure/keyvault-keys";
528
+
```
529
+
530
+
3. Within the `index.js` file, create client objects for the Azure services your app will connect to. The following examples connect to Blob Storage, Cosmos DB, and Azure SQL using the corresponding SDK classes.
531
+
532
+
```javascript
533
+
// Get the first user-assigned managed identity ID to connect to shared storage
// Open a connection to Azure SQL using a managed identity with mssql package
571
+
// mssql reads the environment variables to get the managed identity
572
+
const server = process.env.AZURE_SQL_SERVER;
573
+
const database = process.env.AZURE_SQL_DATABASE;
574
+
const port = parseInt(process.env.AZURE_SQL_PORT);
575
+
const type = process.env.AZURE_SQL_AUTHENTICATIONTYPE;
576
+
577
+
const config = {
578
+
server,
579
+
port,
580
+
database,
581
+
authentication: {
582
+
type // <---- Passwordless connection
583
+
},
584
+
options: {
585
+
encrypt: true
586
+
}
587
+
};
588
+
589
+
await sql.connect(sqlConfig);
590
+
```
591
+
478
592
---
479
593
480
594
You can also associate a user-assigned managed identity as well as a system-assigned managed identity to a resource simultaneously. This can be useful in scenarios where all of the apps require access to the same shared services, but one of the apps also has a very specific dependency on an additional service. Using a system-assigned identity also ensures that the identity tied to that specific app is deleted when the app is deleted, which can help keep your environment clean.
0 commit comments