author | ms.service | ms.topic | ms.date | ms.author |
---|---|---|---|---|
dominicbetts |
iot-hub |
include |
10/26/2018 |
dobett |
Azure Active Directory must authenticate all the tasks that you perform on resources using the Azure Resource Manager. The example shown here uses password authentication, for other approaches see Authenticating Azure Resource Manager requests.
-
Add the following code to the Main method in Program.cs to retrieve a token from Azure AD using the application id and password.
var authContext = new AuthenticationContext(string.Format ("https://login.microsoftonline.com/{0}", tenantId)); var credential = new ClientCredential(applicationId, password); AuthenticationResult token = authContext.AcquireTokenAsync ("https://management.core.windows.net/", credential).Result; if (token == null) { Console.WriteLine("Failed to obtain the token"); return; }
-
Create a ResourceManagementClient object that uses the token by adding the following code to the end of the Main method:
var creds = new TokenCredentials(token.AccessToken); var client = new ResourceManagementClient(creds); client.SubscriptionId = subscriptionId;
-
Create, or obtain a reference to, the resource group you are using:
var rgResponse = client.ResourceGroups.CreateOrUpdate(rgName, new ResourceGroup("East US")); if (rgResponse.Properties.ProvisioningState != "Succeeded") { Console.WriteLine("Problem creating resource group"); return; }