From 93d3915fdc5b637662d4e6aaaf0df1a2b2fb44b5 Mon Sep 17 00:00:00 2001 From: Suwath Ch Date: Wed, 14 Jan 2015 00:34:54 -0800 Subject: [PATCH] support auto-login --- .../AADAuthentication/BaseAuthHelper.cs | 10 ++++----- ARMClient.Console/Program.cs | 21 +++++++++++-------- ARMClient.Library/ARMClient.cs | 2 +- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/ARMClient.Authentication/AADAuthentication/BaseAuthHelper.cs b/ARMClient.Authentication/AADAuthentication/BaseAuthHelper.cs index 6173fe8..d60498f 100644 --- a/ARMClient.Authentication/AADAuthentication/BaseAuthHelper.cs +++ b/ARMClient.Authentication/AADAuthentication/BaseAuthHelper.cs @@ -87,7 +87,7 @@ namespace ARMClient.Authentication.AADAuthentication { foreach (var authResult in authResults) { - Console.WriteLine(authResult.UserInfo.UserId); + Trace.WriteLine(authResult.UserInfo.UserId); } throw new InvalidOperationException("Multiple users found. Please specify user argument!"); @@ -131,10 +131,10 @@ namespace ARMClient.Authentication.AADAuthentication tenantId = tenantId }; - Console.WriteLine("App: {0}, Tenant: {1}", appId, tenantId); + Trace.WriteLine(String.Format("App: {0}, Tenant: {1}", appId, tenantId)); var subscriptions = await GetSubscriptions(authResult); - Console.WriteLine("\tThere are {0} subscriptions", subscriptions.Length); + Trace.WriteLine(String.Format("\tThere are {0} subscriptions", subscriptions.Length)); info.subscriptions = subscriptions.Select(subscription => new SubscriptionCacheInfo { @@ -144,7 +144,7 @@ namespace ARMClient.Authentication.AADAuthentication foreach (var subscription in subscriptions) { - Console.WriteLine("\tSubscription {0} ({1})", subscription.subscriptionId, subscription.displayName); + Trace.WriteLine(String.Format("\tSubscription {0} ({1})", subscription.subscriptionId, subscription.displayName)); } tenantCache[tenantId] = info; @@ -336,7 +336,7 @@ namespace ARMClient.Authentication.AADAuthentication protected async Task> GetTokenForTenants(Dictionary tokenCache, AuthenticationResult authResult) { var tenantIds = await GetTenantIds(authResult); - Console.WriteLine("User {0} belongs to {1} tenants", authResult.UserInfo.UserId, tenantIds.Length); + Trace.WriteLine(string.Format("User {0} belongs to {1} tenants", authResult.UserInfo.UserId, tenantIds.Length)); var tenantCache = this.TenantStorage.GetCache(); foreach (var tenantId in tenantIds) diff --git a/ARMClient.Console/Program.cs b/ARMClient.Console/Program.cs index 456f42c..07dc27c 100644 --- a/ARMClient.Console/Program.cs +++ b/ARMClient.Console/Program.cs @@ -106,17 +106,25 @@ namespace ARMClient || String.Equals(verb, "post", StringComparison.OrdinalIgnoreCase)) { var path = _parameters.Get(1, keyName: "url"); + var verbose = _parameters.Get("-verbose", requires: false) != null; var baseUri = new Uri(ARMClient.Authentication.Constants.CSMUrls[(int)AzureEnvironments.Prod]); var uri = new Uri(baseUri, path); - // TODO: acquire token - EnsureTokenCache(persistentAuthHelper); + if (!verbose) + { + Trace.Listeners.Clear(); + } + + if (!persistentAuthHelper.IsCacheValid()) + { + persistentAuthHelper.AzureEnvironments = GetAzureEnvironments(uri); + persistentAuthHelper.AcquireTokens().Wait(); + } var env = persistentAuthHelper.AzureEnvironments; baseUri = new Uri(ARMClient.Authentication.Constants.CSMUrls[(int)env]); uri = new Uri(baseUri, path); var content = ParseHttpContent(verb, _parameters); - var verbose = _parameters.Get("-verbose", requires: false) != null; _parameters.ThrowIfUnknown(); var subscriptionId = GetSubscription(uri); @@ -141,14 +149,9 @@ namespace ARMClient PrintUsage(); return 1; } - catch (CommandLineException ex) - { - Console.WriteLine(ex.Message); - return -1; - } catch (Exception ex) { - Console.WriteLine(ex); + Console.WriteLine(ex.GetBaseException().Message); return -1; } } diff --git a/ARMClient.Library/ARMClient.cs b/ARMClient.Library/ARMClient.cs index 19199d1..d3b1039 100644 --- a/ARMClient.Library/ARMClient.cs +++ b/ARMClient.Library/ARMClient.cs @@ -166,7 +166,7 @@ namespace ARMClient.Library using (var client = new HttpClient(new HttpClientHandler())) { client.DefaultRequestHeaders.Add("Authorization", this._authorizationHeader); - client.DefaultRequestHeaders.Add("User-Agent", "CSMClient-" + Environment.MachineName); + client.DefaultRequestHeaders.Add("User-Agent", "ARMClient-" + Environment.MachineName); client.DefaultRequestHeaders.Add("Accept", "application/json"); HttpResponseMessage response = null;