Skip to content

Commit 28b12df

Browse files
committed
Added Entra for .NET
1 parent 420b80b commit 28b12df

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

articles/iot-hub/how-to-module-twins.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ This article shows you how to develop two types of applications:
3535

3636
* If your application uses the MQTT protocol, make sure that **port 8883** is open in your firewall. The MQTT protocol communicates over port 8883. This port may be blocked in some corporate and educational network environments. For more information and ways to work around this issue, see [Connecting to IoT Hub (MQTT)](../iot/iot-mqtt-connect-to-iot-hub.md#connecting-to-iot-hub).
3737

38-
* Language SDK requirements:
39-
4038
:::zone pivot="programming-language-csharp"
4139

4240
[!INCLUDE [iot-hub-howto-module-twins-dotnet](../../includes/iot-hub-howto-module-twins-dotnet.md)]

includes/iot-hub-howto-module-twins-dotnet.md

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,19 @@ using Microsoft.Azure.Devices;
151151
using Microsoft.Azure.Devices.Shared;
152152
```
153153

154-
### Connect to IoT hub
154+
### Connect to IoT Hub
155155

156-
Connect a backend application to IoT hub using [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.registrymanager.createfromconnectionstring).
156+
You can connect a backend service to IoT Hub using the following methods:
157+
158+
* Shared access policy
159+
* Microsoft Entra
160+
* X.509 certificate
157161

158-
The SDK methods in this section require these shared access policy permissions:
162+
#### Connect using a shared access policy
159163

160-
* **Registry Write** - required to add a module (or device) to the IoT Hub registry
161-
* **Service Connect** - required to add desired properties to a module
164+
Connect a backend application to IoT hub using [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.registrymanager.createfromconnectionstring).
162165

163-
As a parameter to `CreateFromConnectionString`, supply a shared access policy connection string that includes these permissions. For more information about shared access policies, see [Control access to IoT Hub with shared access signatures](/azure/iot-hub/authenticate-authorize-sas).
166+
The `UpdateModuleAsync` method used in this section requires the **Service Connect** shared access policy permission to add desired properties to a module. As a parameter to `CreateFromConnectionString`, supply a shared access policy connection string that includes **Service Connect** permission. For more information about shared access policies, see [Control access to IoT Hub with shared access signatures](/azure/iot-hub/authenticate-authorize-sas).
164167

165168
For example:
166169

@@ -170,11 +173,40 @@ static string connectionString = "{IoT hub shared access policy connection strin
170173
registryManager = RegistryManager.CreateFromConnectionString(connectionString);
171174
```
172175

176+
#### Connect using Microsoft Entra
177+
178+
Use [DefaultAzureCredential](https://learn.microsoft.com/en-us/dotnet/api/azure.identity.defaultazurecredential) to use Microsoft Entra to authenticate a connection to IoT Hub. `DefaultAzureCredential` supports different authentication mechanisms and determines the appropriate credential type based of the environment it is executing in. It attempts to use multiple credential types in an order until it finds a working credential. For more information on setting up Entra for IoT Hub, see [Control access to IoT Hub by using Microsoft Entra ID](https://learn.microsoft.com/en-us/azure/iot-hub/authenticate-authorize-azure-ad).
179+
180+
To supply parameters to `DefaultAzureCredential`, first create an Entra app registration that contains the Azure client secret, client ID, and tenant ID. An Entra profile requires [IoT Hub Twin Contributor](https://learn.microsoft.com/en-us/azure/role-based-access-control/built-in-roles/internet-of-things#iot-hub-twin-contributor) to enable read and write access to all IoT Hub device and module twins.
181+
182+
In this example, the Entra app registration client secret, client ID, and tenant ID are added to environment variables. These environment variables are used by `DefaultAzureCredential` to authenticate the application.
183+
184+
```csharp
185+
string clientSecretValue = "xxxxxxxxxxxxxxx";
186+
string clientID = "xxxxxxxxxxxxxx";
187+
string tenantID = "xxxxxxxxxxxxx";
188+
189+
Environment.SetEnvironmentVariable("AZURE_CLIENT_SECRET", clientSecretValue);
190+
Environment.SetEnvironmentVariable("AZURE_CLIENT_ID", clientID);
191+
Environment.SetEnvironmentVariable("AZURE_TENANT_ID", tenantID);
192+
193+
TokenCredential tokenCredential = new DefaultAzureCredential();
194+
```
195+
196+
The [TokenCredential](/dotnet/api/azure.core.tokencredential) can then be passed to [ServiceClient.Create](https://review.learn.microsoft.com/en-us/dotnet/api/microsoft.azure.devices.serviceclient.create?#microsoft-azure-devices-serviceclient-create(system-string-azure-core-tokencredential-microsoft-azure-devices-transporttype-microsoft-azure-devices-serviceclienttransportsettings-microsoft-azure-devices-serviceclientoptions)) to create a [ServiceClient](/dotnet/api/microsoft.azure.devices.serviceclient) connection object.
197+
198+
For example:
199+
200+
```csharp
201+
string hostname = "xxxxxxxxxx.azure-devices.net";
202+
using var serviceClient = ServiceClient.Create(hostname, tokenCredential, TransportType.Amqp);
203+
```
204+
173205
### Read and update module identity fields
174206

175207
Call [GetModuleAsync](/dotnet/api/microsoft.azure.devices.registrymanager.getmoduleasync) to retrieve current module identity twin fields into a [Module](/dotnet/api/microsoft.azure.devices.module) object.
176208

177-
The `Module` class includes `properties` that correspond to sections of a module identity twin. Use the Module class properties to view and update module identity twin fields. You can use the `Module` object properties to update multiple fields before writing the updates to the device using `UpdateModuleAsync`.
209+
The `Module` class includes `properties` that correspond to sections of a module identity twin. Use the Module class properties to view and update module identity twin fields. You can use the `Module` object properties to update multiple fields before writing the updates to the device using `UpdateModuleAsync`.
178210

179211
After making module identity twin field updates, call [UpdateModuleAsync](/dotnet/api/microsoft.azure.devices.registrymanager.updatemoduleasync) to write `Module` object field updates back to a device. Use `try` and `catch` logic coupled with an error handler to catch incorrectly formatted patch errors from `UpdateModuleAsync`.
180212

0 commit comments

Comments
 (0)