public static AuthenticationResult GetAuthToken(string user, SecureString password, Hashtable modulePrivateData) { string authority = string.Format("https://{0}/common", GetAuthURI(modulePrivateData)); AuthenticationContext authContext = new AuthenticationContext(authority); PlatformParameters platformParams = new PlatformParameters(PromptBehavior.Auto); if (password == null) { UserIdentifier userId = new UserIdentifier(user, UserIdentifierType.OptionalDisplayableId); return(authContext.AcquireTokenAsync(GetGraphURI(modulePrivateData), ClientId, redirectUri, platformParams, userId).Result); } else { UserPasswordCredential userCreds = new UserPasswordCredential(user, password); return(AuthenticationContextIntegratedAuthExtensions.AcquireTokenAsync(authContext, GetGraphURI(modulePrivateData), ClientId, userCreds).Result); } }
static async Task runSample(string tenantId, string clientId, string subscriptionId, string userName, string password, string location, string armEndpoint) { var resourceGroup1Name = SdkContext.RandomResourceName("rgDotnetSdk", 24); var resourceGroup2Name = SdkContext.RandomResourceName("rgDotnetSdk", 24); Console.WriteLine("Get credential token"); var adSettings = getActiveDirectoryServiceSettings(armEndpoint); // Authenticate with ADAL directly. Fluent packages don't support UserPass auth var tokenCache = new TokenCache(); var context = new AuthenticationContext(authority: adSettings.AuthenticationEndpoint.ToString(), validateAuthority: false, tokenCache: tokenCache); var cred = new UserPasswordCredential(userName, password); var token = await AuthenticationContextIntegratedAuthExtensions.AcquireTokenAsync(ctx : context, resource : adSettings.TokenAudience.ToString(), clientId : clientId, userCredential : cred).ConfigureAwait(continueOnCapturedContext: false); var credentials = await UserTokenProvider.CreateCredentialsFromCache(clientId : clientId, domain : tenantId, username : userName, cache : tokenCache, serviceSettings : adSettings).ConfigureAwait(continueOnCapturedContext: false); Console.WriteLine("Instantiate resource management client"); var rmClient = GetResourceManagementClient(new Uri(armEndpoint), credentials, subscriptionId); // Create resource group. try { Console.WriteLine(String.Format("Creating a resource group with name:{0}", resourceGroup1Name)); var rmCreateTask = rmClient.ResourceGroups.CreateOrUpdateWithHttpMessagesAsync( resourceGroup1Name, new Profile2018ResourceManager.Models.ResourceGroup { Location = location }); rmCreateTask.Wait(); } catch (Exception ex) { Console.WriteLine(String.Format("Could not create resource group {0}. Exception: {1}", resourceGroup1Name, ex.Message)); } // Update the resource group. try { Console.WriteLine(String.Format("Updating the resource group with name:{0}", resourceGroup1Name)); var rmTagTask = rmClient.ResourceGroups.PatchWithHttpMessagesAsync(resourceGroup1Name, new Profile2018ResourceManager.Models.ResourceGroup { Tags = new Dictionary <string, string> { { "DotNetTag", "DotNetValue" } } }); rmTagTask.Wait(); } catch (Exception ex) { Console.WriteLine(String.Format("Could not tag resource group {0}. Exception: {1}", resourceGroup1Name, ex.Message)); } // Create another resource group. try { Console.WriteLine(String.Format("Creating a resource group with name:{0}", resourceGroup2Name)); var rmCreateTask = rmClient.ResourceGroups.CreateOrUpdateWithHttpMessagesAsync( resourceGroup2Name, new Profile2018ResourceManager.Models.ResourceGroup { Location = location }); rmCreateTask.Wait(); } catch (Exception ex) { Console.WriteLine(String.Format("Could not create resource group {0}. Exception: {1}", resourceGroup2Name, ex.Message)); } // List resource groups. try { Console.WriteLine("Listing all resource groups."); var rmListTask = rmClient.ResourceGroups.ListWithHttpMessagesAsync(); rmListTask.Wait(); var resourceGroupResults = rmListTask.Result.Body; foreach (var result in resourceGroupResults) { Console.WriteLine(String.Format("Resource group name:{0}", result.Name)); } } catch (Exception ex) { Console.WriteLine(string.Format("Could not list resource groups. Exception: {0}", ex.Message)); } // Delete a resource group. try { Console.WriteLine(String.Format("Deleting resource group with name:{0}", resourceGroup2Name)); var rmDeleteTask = rmClient.ResourceGroups.DeleteWithHttpMessagesAsync(resourceGroup2Name); rmDeleteTask.Wait(); } catch (Exception ex) { Console.WriteLine(String.Format("Could not delete resource group {0}. Exception: {1}", resourceGroup2Name, ex.Message)); } }