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
You can use the [IoT Hub resource provider REST API](/rest/api/iothub/iothubresource) to create and manage Azure IoT hubs programmatically. This tutorial shows you how to use the IoT Hub resource provider REST API to create an IoT hub from a C# program.
16
+
You can use the [IoT Hub Resource](/rest/api/iothub/iothubresource) REST API to create and manage Azure IoT hubs programmatically. This article shows you how to use the IoT Hub Resource to create an IoT hub using **Postman**. Alternatively, you can use **cURL**. If any of these REST commands fail, find help with the [IoT Hub API common error codes](/rest/api/iothub/common-error-codes).
1. In the Azure PowerShell cmdlet or Azure Cloud Shell, sign in and then retrieve a token with the following command. If you're using Cloud Shell you are already signed in, so skip this step.
31
29
32
-
1. In Visual Studio, create a Visual C# Windows Classic Desktop project using the **Console App (.NET Framework)** project template. Name the project **CreateIoTHubREST**.
30
+
```azurecli-interactive
31
+
az account get-access-token --resource https://management.azure.com
32
+
```
33
+
You should see a response in the console similar to this JSON (except the access token is long):
33
34
34
-
2. In Solution Explorer, right-click on your project and then click **Manage NuGet Packages**.
3. In NuGet Package Manager, check **Include prerelease**, and on the **Browse**page search for **Microsoft.Azure.Management.ResourceManager**. Select the package, click **Install**, in **Review Changes** click**OK**, then click **I Accept** to accept the licenses.
45
+
1. In a new **Postman** request, from the **Auth**tab, select the **Type** dropdown list and choose**Bearer Token**.
37
46
38
-
4. In NuGet Package Manager, search for **Microsoft.IdentityModel.Clients.ActiveDirectory**. Click **Install**, in **Review Changes** click **OK**, then click **I Accept** to accept the license.
39
-
> [!IMPORTANT]
40
-
> The [Microsoft.IdentityModel.Clients.ActiveDirectory](https://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory) NuGet package and Azure AD Authentication Library (ADAL) have been deprecated. No new features have been added since June 30, 2020. We strongly encourage you to upgrade. For more information see the [migration guide](../active-directory/develop/msal-migration.md).
47
+
:::image type="content" source="media/iot-hub-rm-rest/select-bearer-token.png" alt-text="Screenshot that shows how to select the Bearer Token type of authorization in **Postman**.":::
41
48
42
-
5. In Program.cs, replace the existing **using** statements with the following code:
49
+
1. Paste the access token into the field labeled **Token**.
Keep in mind the access token expires after 5-60 minutes, so you may need to generate another one.
57
52
58
-
6. InProgram.cs, addthefollowingstaticvariablesreplacingtheplaceholdervalues. Youmadeanoteof**ApplicationId**, **SubscriptionId**, **TenantId**, and**Password**earlierinthistutorial. **Resourcegroupname**isthenameoftheresourcegroupyouuse when you create the IoT hub. You can use a pre-existing or a new resource group. **IoT Hub name** is the name of the IoT Hub you create, such as **MyIoTHub**. The name of your IoT hub must be globally unique. **Deployment name** is a name for the deployment, such as **Deployment_01**.
1. Select the REST command dropdown list and choose the PUT command. Copy the URL below, replacing the values in the `{}` with your own values. The `{resourceName}` value is the name you'd like for your new IoT hub. Paste the URL into the field next to the PUT command.
65
56
66
-
staticstringrgName="{Resource group name}";
67
-
staticstringiotHubName="{IoT Hub name including your initials}";
68
-
```
57
+
```rest
58
+
PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Devices/IotHubs/{resourceName}?api-version=2021-04-12
:::image type="content" source="media/iot-hub-rm-rest/add-body-for-put.png" alt-text="Screenshot that shows how to add JSON to the body of your request in Postman.":::
77
68
78
-
1. AddthefollowingmethodtoProgram.cs:
69
+
1.Copy the following JSON, replacing values in `<>` with your own. Paste the JSON into the box in **Postman** on the **Body** tab. Make sure your IoT hub name matches the one in your PUT URL. Change the location to your location (the location assigned to your resource group).
1. Select **Send** to send your request and create a new IoT hub. A successful request will return a **201 Created** response with a JSON printout of your IoT hub specifications. You can save your request if you're using **Postman**.
To see all the specifications of your new IoT hub, use a GET request. You can use the same URL that you used with the PUT request, but must erase the **Body** of that request (if not already blank) because a GET request can't have a body. Here's the GET request template:
GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Devices/IotHubs/{resourceName}?api-version=2018-04-01
Updating is as simple as using the same PUT request from when we created the IoT hub and editing the JSON body to contain parameters of your choosing. Edit the body of the request by adding a **tags** property, then run the PUT request.
PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Devices/IotHubs/{resourceName}?api-version=2018-04-01
The response will show the new tag added in the console. Remember, you may need to refresh your access token if too much time has passed since the last time you generated one.
If you're only testing, you might want to clean up your resources and delete your new IoT hub, by sending a DELETE request. be sure to replace the values in `{}` with your own values. The `{resourceName}` value is the name of your IoT hub.
163
132
164
-
4. ToverifythatyourapplicationaddedthenewIoThub, visitthe [Azureportal](https://portal.azure.com/) and view your list of resources. Alternatively, use the **Get-AzResource** PowerShell cmdlet.
>ThisexampleapplicationaddsanS1StandardIoTHubforwhichyouarebilled. Whenyouarefinished, youcandeletetheIoThubthroughthe [Azureportal](https://portal.azure.com/) or by using the **Remove-AzResource** PowerShell cmdlet when you are finished.
137
+
See the [DELETE command in the IoT Hub Resource](/rest/api/iothub/iot-hub-resource/delete?tabs=HTTP).
168
138
169
139
## Next steps
170
140
@@ -182,4 +152,4 @@ To learn more about developing for IoT Hub, see the following articles:
182
152
183
153
To further explore the capabilities of IoT Hub, see:
Copy file name to clipboardExpand all lines: includes/iot-hub-prepare-resource-manager.md
+17-20Lines changed: 17 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ You must authenticate all the operations that you perform on resources using the
10
10
11
11
Install the [Azure PowerShell cmdlets][lnk-powershell-install] before you continue.
12
12
13
-
The following steps show how to set up password authentication for an AD application using PowerShell. You can run these commands in a standard PowerShell session.
13
+
The following steps show how to set up authentication for your app to register with Azure Active Directory. You can run these commands in a standard PowerShell session. Registering with Azure Active Directory is necessary to authenticate any future REST calls. For more information, see [How and why applications are added to Azure AD](/azure/active-directory/develop/active-directory-how-applications-are-added).
14
14
15
15
1. Sign in to your Azure subscription using the following command:
16
16
@@ -26,47 +26,44 @@ The following steps show how to set up password authentication for an AD applica
26
26
Get-AzSubscription
27
27
```
28
28
29
-
Select the subscription you want to use. You can use either the subscription name or ID from the output of the previous command.
29
+
Select the subscription you want to use. You can use either the subscription name or `Id` from the output of the previous command.
1. Create a new Azure Active Directory application using the following command, replacing the placeholders:
37
+
1. Create a new Azure Active Directory application using the following command, replacing these placeholders with your own values:
38
38
39
39
***{Display name}:** a display name for your application such as **MySampleApp**
40
-
***{Home page URL}:**the URL of the home page of your app such as **http:\//mysampleapp/home**. This URL doesn't need to point to a real application.
41
-
***{Application identifier}:** A unique identifier such as **http:\//mysampleapp**. This URL doesn't need to point to a real application.
42
-
***{Password}:** A password that you use to authenticate with your app.
40
+
***{Application identifier}:**A unique identifier such as your primary domain. To find the primary domain associated with your subscription, go to the [Azure portal](https://ms.portal.azure.com/#home) in the **Azure Active Directory** service on its **Overview page** and find **Primary domain**. See the different domain possibilities in the [Azure Active Directory app manifest](/azure/active-directory/develop/reference-app-manifest#identifieruris-attribute). Be sure to add `/your-id` at the end of your domain (`your-Id` can be any name), for example, `"https://microsoft.onmicrosoft.com/my-unique-ad-app"`.
41
+
42
+
:::image type="content" source="/includes/media/iot-hub-prepare-resource-manager/find-domain.png" alt-text="Screenshot showing location of your Primary domain in the Azure portal.":::
To understand roles and permissions, see [Create or update Azure custom roles using Azure PowerShell](/azure/role-based-access-control/custom-roles-powershell).
59
+
62
60
With your new Azure AD application, you can now authenticate from your custom C# application.
63
61
64
62
You need the following values later in this tutorial:
0 commit comments