public void UserCredentialsPopsDialog() { var cache = new TestTokenCache(); var settings = ActiveDirectoryServiceSettings.Azure; var credentials = UserTokenProvider.LoginWithPromptAsync(this._domain, ActiveDirectoryClientSettings.UsePromptOnly("1950a258-227b-4e31-a9cf-717495945fc2", new Uri("urn:ietf:wg:oauth:2.0:oob")), settings, this._username, cache).GetAwaiter().GetResult(); var client = new HttpClient(); var request = new HttpRequestMessage(HttpMethod.Get, new Uri("https://management.azure.com/subscriptions?api-version=2014-04-01-preview")); credentials.ProcessHttpRequestAsync(request, CancellationToken.None).Wait(); Assert.NotNull(request.Headers.Authorization); var response = client.SendAsync(request).ConfigureAwait(false).GetAwaiter().GetResult(); Assert.Equal(HttpStatusCode.OK, response.StatusCode); // Repeat with PromptBehavior.Never credentials = UserTokenProvider.LoginWithPromptAsync(this._domain, ActiveDirectoryClientSettings.UseCacheOrCookiesOnly("1950a258-227b-4e31-a9cf-717495945fc2", new Uri("urn:ietf:wg:oauth:2.0:oob")), settings, this._username, cache).GetAwaiter().GetResult(); request = new HttpRequestMessage(HttpMethod.Get, new Uri("https://management.azure.com/subscriptions?api-version=2014-04-01-preview")); credentials.ProcessHttpRequestAsync(request, CancellationToken.None).Wait(); Assert.NotNull(request.Headers.Authorization); response = client.SendAsync(request).ConfigureAwait(false).GetAwaiter().GetResult(); Assert.Equal(HttpStatusCode.OK, response.StatusCode); // Repeat with getting tokens strictly from cache credentials = UserTokenProvider.CreateCredentialsFromCache("1950a258-227b-4e31-a9cf-717495945fc2", this._domain, this._username, cache).GetAwaiter().GetResult(); request = new HttpRequestMessage(HttpMethod.Get, new Uri("https://management.azure.com/subscriptions?api-version=2014-04-01-preview")); credentials.ProcessHttpRequestAsync(request, CancellationToken.None).Wait(); Assert.NotNull(request.Headers.Authorization); response = client.SendAsync(request).ConfigureAwait(false).GetAwaiter().GetResult(); Assert.Equal(HttpStatusCode.OK, response.StatusCode); }
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)); } }