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
description: See how to write authentication code in a client application
6
6
author: baanders
7
7
ms.author: baanders # Microsoft employees only
8
-
ms.date: 4/22/2020
8
+
ms.date: 10/7/2020
9
9
ms.topic: how-to
10
10
ms.service: digital-twins
11
-
ms.custom: devx-track-js
12
11
13
12
# Optional fields. Don't forget to remove # if you need a field.
14
13
# ms.custom: can-be-multiple-comma-separated
@@ -18,60 +17,66 @@ ms.custom: devx-track-js
18
17
19
18
# Write client app authentication code
20
19
21
-
After you [set up an Azure Digital Twins instance and authentication](how-to-set-up-instance-portal.md), you can create a client application that you will use to interact with the instance. Once you have set up a starter client project, this article shows you**how to write code in that client app to authenticate it** against the Azure Digital Twins instance.
20
+
After you [set up an Azure Digital Twins instance and authentication](how-to-set-up-instance-portal.md), you can create a client application that you will use to interact with the instance. Once you have set up a starter client project, you'll need to **write code in that client app to authenticate it** against the Azure Digital Twins instance.
22
21
23
-
There are two approaches to sample code in this article. You can use the one that's right for you, depending on your language of choice:
24
-
* The first section of sample code uses the Azure Digital Twins .NET (C#) SDK. The SDK is part of the Azure SDK for .NET, and is located here: [*Azure IoT Digital Twin client library for .NET*](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/digitaltwins/Azure.DigitalTwins.Core). There are also supported SDKs for [Java](https://search.maven.org/artifact/com.azure/azure-digitaltwins-core/1.0.0-beta.1/jar) and [JavaScript](https://www.npmjs.com/package/@azure/digital-twins/v/1.0.0-preview.1), which can be used in a similar way.
25
-
* The second section of sample code is for users not using a provided SDK, and instead using AutoRest-generated SDKs in other languages. For more information on this strategy, see [*How-to: Create custom SDKs for Azure Digital Twins with AutoRest*](how-to-create-custom-sdks.md).
22
+
Azure Digital Twins performs authentication using [Azure AD Security Tokens based on OAUTH 2.0](../active-directory/develop/security-tokens.md#json-web-tokens-jwts-and-claims). To authenticate your SDK, you'll need to get a bearer token with the right permissions to Azure Digital Twins, and pass it along with your API calls.
26
23
27
-
You can also read more about the APIs and SDKs for Azure Digital Twins in[*How-to: Use the Azure Digital Twins APIs and SDKs*](how-to-use-apis-sdks.md).
24
+
This article describes how to obtain credentials using the `Azure.Identity` client library. While this article shows code examples in C#, such as what you'd write for the [.NET (C#) SDK](https://www.nuget.org/packages/Azure.DigitalTwins.Core), you can use a version of `Azure.Identity` regardless of what SDK you're using (for more on the SDKs available for Azure Digital Twins, see[*How-to: Use the Azure Digital Twins APIs and SDKs*](how-to-use-apis-sdks.md)).
28
25
29
26
## Prerequisites
30
27
31
28
First, complete the setup steps in [*How-to: Set up an instance and authentication*](how-to-set-up-instance-portal.md). This will ensure you have an Azure Digital Twins instance, your user has access permissions, and you've set up permissions for client applications. After all this setup, you are ready to write client app code.
32
29
33
30
To proceed, you will need a client app project in which you write your code. If you don't already have a client app project set up, create a basic project in your language of choice to use with this tutorial.
34
31
35
-
## Authentication and client creation: .NET (C#) SDK
32
+
## Common authentication methods with Azure.Identity
36
33
37
-
This section shows an example in C# for using the provided .NET SDK.
34
+
`Azure.Identity` is a client library that provides several credential-obtaining methods that you can use to get a bearer token and authenticate with your SDK. Although this article gives examples in C#, you can view `Azure.Identity` for several languages, including...
First, include the following packages in your project in order to use the .NET SDK and authentication tools for this how-to:
40
-
*`Azure.DigitalTwins.Core`
41
-
*`Azure.Identity`
40
+
Three common credential-obtaining methods in `Azure.Identity` are:
41
+
*[DefaultAzureCredential](https://docs.microsoft.com/dotnet/api/azure.identity.defaultazurecredential?view=azure-dotnet&preserve-view=true) provides a default `TokenCredential` authentication flow for applications that will be deployed to Azure, and is **the recommended choice for local development**. It also can be enabled to try the other two methods recommended in this article; it wraps `ManagedIdentityCredential` and can access `InteractiveBrowserCredential` with a configuration variable.
42
+
*[ManagedIdentityCredential](https://docs.microsoft.com/dotnet/api/azure.identity.managedidentitycredential?view=azure-dotnet&preserve-view=true) works great in cases where you need [managed identities (MSI)](../active-directory/managed-identities-azure-resources/overview.md), and is a good candidate for working with Azure Functions and deploying to Azure services.
43
+
*[InteractiveBrowserCredential](https://docs.microsoft.com/dotnet/api/azure.identity.interactivebrowsercredential?view=azure-dotnet&preserve-view=true) is intended for interactive applications, and can be used to create an authenticated SDK client
44
+
45
+
The following example shows how to use each of these with the .NET (C#) SDK.
46
+
47
+
## Authentication examples: .NET (C#) SDK
48
+
49
+
This section shows an example in C# for using the provided .NET SDK to write authentication code.
42
50
43
-
Depending on your tools of choice, you can include the packages using the Visual Studio package manager or the `dotnet` command line tool.
51
+
First, include the SDK package `Azure.DigitalTwins.Core` and the `Azure.Identity` package in your project. Depending on your tools of choice, you can include the packages using the Visual Studio package manager or the `dotnet` command line tool.
44
52
45
-
You'll also need the following using statements:
53
+
You'll also need to add the following using statements to your project code:
46
54
47
55
```csharp
48
56
usingAzure.Identity;
49
57
usingAzure.DigitalTwins.Core;
50
58
```
51
-
To authenticate with the .NET SDK, use one of the credential-obtaining methods that are defined in the [Azure.Identity](https://docs.microsoft.com/dotnet/api/azure.identity?view=azure-dotnet&preserve-view=true) library. Here are two that are commonly used (even together in the same application):
52
59
53
-
*[InteractiveBrowserCredential](https://docs.microsoft.com/dotnet/api/azure.identity.interactivebrowsercredential?view=azure-dotnet&preserve-view=true) is intended for interactive applications, and can be used to create an authenticated SDK client
54
-
*[ManagedIdentityCredential](https://docs.microsoft.com/dotnet/api/azure.identity.managedidentitycredential?view=azure-dotnet&preserve-view=true) works great in cases where you need managed identities (MSI), and is a good candidate for working with Azure Functions
60
+
Then, add code to obtain credentials using one of the methods in `Azure.Identity`.
55
61
56
-
### InteractiveBrowserCredential method
57
-
The [InteractiveBrowserCredential](https://docs.microsoft.com/dotnet/api/azure.identity.interactivebrowsercredential?view=azure-dotnet&preserve-view=true) method is intended for interactive applications and will bring up a web browser for authentication.
62
+
### DefaultAzureCredential method
63
+
64
+
[DefaultAzureCredential](https://docs.microsoft.com/dotnet/api/azure.identity.defaultazurecredential?view=azure-dotnet&preserve-view=true) provides a default `TokenCredential` authentication flow for applications that will be deployed to Azure, and is **the recommended choice for local development**.
58
65
59
-
To use the interactive browser credentials to create an authenticated SDK client, add this code:
66
+
To use the default Azure credentials, you'll need the Azure Digital Twins instance's URL ([instructions to find](how-to-set-up-instance-portal.md#verify-success-and-collect-important-values)).
67
+
68
+
Here is a code sample to add a `DefaultAzureCredential` to your project:
60
69
61
70
```csharp
62
-
// Your client / app registration ID
63
-
privatestaticstringclientId="<your-client-ID>";
64
-
// Your tenant / directory ID
65
-
privatestaticstringtenantId="<your-tenant-ID>";
66
71
// The URL of your instance, starting with the protocol (https://)
> While you can place the client ID, tenant ID and instance URL directly into the code as shown above, it's a good idea to have your code get these values from a configuration file or environment variable instead.
85
-
86
88
### ManagedIdentityCredential method
87
-
The [ManagedIdentityCredential](https://docs.microsoft.com/dotnet/api/azure.identity.managedidentitycredential?view=azure-dotnet&preserve-view=true) method works great in cases where you need [managed identities (MSI)](https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/overview)—for example, when working with Azure Functions.
89
+
90
+
The [ManagedIdentityCredential](https://docs.microsoft.com/dotnet/api/azure.identity.managedidentitycredential?view=azure-dotnet&preserve-view=true) method works great in cases where you need [managed identities (MSI)](https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/overview)—for example, when working with Azure Functions.
91
+
92
+
This means that you may use `ManagedIdentityCredential` in the same project as `DefaultAzureCredential` or `InteractiveBrowserCredential`, to authenticate a different part of the project.
93
+
94
+
To use the default Azure credentials, you'll need the Azure Digital Twins instance's URL ([instructions to find](how-to-set-up-instance-portal.md#verify-success-and-collect-important-values)).
95
+
88
96
In an Azure function, you can use the managed identity credentials like this:
The [InteractiveBrowserCredential](https://docs.microsoft.com/dotnet/api/azure.identity.interactivebrowsercredential?view=azure-dotnet&preserve-view=true) method is intended for interactive applications and will bring up a web browser for authentication. You can use this instead of `DefaultAzureCredential` in cases where you require interactive authentication.
108
+
109
+
To use the interactive browser credentials, you will need an **app registration** that has permissions to the Azure Digital Twins APIs. For steps on how to set up this app registration, see the [*Set up access permissions for client applications*](how-to-set-up-instance-portal.md#set-up-access-permissions-for-client-applications) section of *How-to: Set up an instance and authentication*. Once the app registration is set up, you'll need...
110
+
* the app registration's *Application (client) ID*
111
+
* the app registration's *Directory (tenant) ID*
112
+
* the Azure Digital Twins instance's URL ([instructions to find](how-to-set-up-instance-portal.md#verify-success-and-collect-important-values))
113
+
114
+
Here is an example of the code to create an authenticated SDK client using `InteractiveBrowserCredential`.
115
+
116
+
```csharp
117
+
// Your client / app registration ID
118
+
privatestaticstringclientId="<your-client-ID>";
119
+
// Your tenant / directory ID
120
+
privatestaticstringtenantId="<your-tenant-ID>";
121
+
// The URL of your instance, starting with the protocol (https://)
Console.WriteLine($"Authentication or client creation error: {e.Message}");
134
+
Environment.Exit(0);
135
+
}
136
+
```
137
+
138
+
>[!NOTE]
139
+
> While you can place the client ID, tenant ID and instance URL directly into the code as shown above, it's a good idea to have your code get these values from a configuration file or environment variable instead.
140
+
141
+
#### Other notes about authenticating Azure Functions
142
+
97
143
See [*How-to: Set up an Azure function for processing data*](how-to-create-azure-function.md) for a more complete example that explains some of the important configuration choices in the context of functions.
98
144
99
145
Also, to use authentication in a function, remember to:
* Use [environment variables](https://docs.microsoft.com/sandbox/functions-recipes/environment-variables?tabs=csharp) as appropriate
102
148
* Assign permissions to the functions app that enable it to access the Digital Twins APIs. For more information on Azure Functions processes, see [*How-to: Set up an Azure function for processing data*](how-to-create-azure-function.md).
103
149
104
-
## Authentication with an AutoRest-generated SDK
105
-
106
-
If you are not using one of the provided SDKs (.NET, Java, JavaScript), you may opt to build an SDK library in a language of your choice, as described in [*How-to: Create custom SDKs for Azure Digital Twins with AutoRest*](how-to-create-custom-sdks.md).
107
-
108
-
This section explains how to authenticate in that case.
109
-
110
-
### Prerequisites
111
-
112
-
First, you should complete the steps to create a custom SDK with AutoRest, using the steps in [*How-to: Create custom SDKs for Azure Digital Twins with AutoRest*](how-to-create-custom-sdks.md).
113
-
114
-
This example uses a Typescript SDK generated with AutoRest. As a result, it also requires:
To authenticate an app with Azure services, you can use the following minimal code within your client app.
121
-
122
-
You will need your *Application (client) ID* and *Directory (tenant) ID* from earlier, as well as the URL of your Azure Digital Twins instance.
123
-
124
-
> [!TIP]
125
-
> The Azure Digital Twins instance's URL is made by adding *https://* to the beginning of your Azure Digital Twins instance's *hostName*. To see the *hostName*, along with all the properties of your instance, you can run `az dt show --dt-name <your-Azure-Digital-Twins-instance>`. You can use the `az account show --query tenantId` command to see your *Directory (tenant) ID*.
Note again that where the code above places the client ID, tenant ID and instance URL directly into the code for simplicity, it's a good idea to have your code get these values from a configuration file or environment variable instead.
150
+
## Other credential methods
200
151
201
-
MSAL has many more options you can use, to implement things like caching and other authentication flows. For more information on this, see [*Overview of Microsoft Authentication Library (MSAL)*](../active-directory/develop/msal-overview.md).
152
+
If the highlighted authentication scenarios above do not cover the needs of your app, you can explore other types of authentication offered in the [**Microsoft identity platform**](../active-directory/develop/v2-overview.md#getting-started). The documentation for this platform covers additional authentication scenarios, organized by application type.
202
153
203
154
## Next steps
204
155
205
156
Read more about how security works in Azure Digital Twins:
206
157
*[*Concepts: Security for Azure Digital Twins solutions*](concepts-security.md)
207
158
208
159
Or, now that authentication is set up, move on to creating models in your instance:
0 commit comments