Ejemplo n.º 1
0
        private void TenantCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string selectedTenantId = this.TenantCB.SelectedValue as string;

            if (!string.IsNullOrWhiteSpace(selectedTenantId))
            {
                Dictionary <string, TenantCacheInfo> tenants = this._authHelper.GetTenants();

                TenantCacheInfo tenant = tenants[selectedTenantId];
                this.SubscriptionCB.ItemsSource       = tenant.subscriptions;
                this.SubscriptionCB.DisplayMemberPath = "displayName";
                this.SubscriptionCB.SelectedValuePath = "subscriptionId";

                if (tenant.subscriptions.Length > 0)
                {
                    this.SubscriptionCB.SelectedIndex = 0;
                }

                Logger.InfoLn("{0} subscription found under tenant '{1}'", tenant.subscriptions.Length, tenant.displayName);
            }

            this.UpdateCmdText();
        }
Ejemplo n.º 2
0
        protected async Task <Dictionary <string, TenantCacheInfo> > GetTokenForTenants(CustomTokenCache tokenCache, TokenCacheInfo cacheInfo,
                                                                                        string appId = null, string appKey = null, string username = null, string password = null)
        {
            var recentInfo = cacheInfo;
            var tenantIds  = await GetTenantIds(cacheInfo);

            if (!tenantIds.Contains(cacheInfo.TenantId))
            {
                var list = tenantIds.ToList();
                list.Insert(0, cacheInfo.TenantId);
                tenantIds = list.ToArray();
            }

            var tenantCache = this.TenantStorage.GetCache();

            foreach (var tenantId in tenantIds)
            {
                var info = new TenantCacheInfo
                {
                    tenantId    = tenantId,
                    displayName = "unknown",
                    domain      = tenantId
                };

                TokenCacheInfo result = null;
                try
                {
                    if (!String.IsNullOrEmpty(appId) && !String.IsNullOrEmpty(appKey))
                    {
                        result = GetAuthorizationResultBySpn(tokenCache, tenantId: tenantId, appId: appId, appKey: appKey, resource: Constants.CSMResources[(int)AzureEnvironments]);
                    }
                    else if (!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password))
                    {
                        result = GetAuthorizationResultByUpn(tokenCache, tenantId: tenantId, username: username, password: password, resource: Constants.CSMResources[(int)AzureEnvironments]);
                    }
                    else
                    {
                        result = await GetAuthorizationResult(tokenCache, tenantId : tenantId, user : cacheInfo.DisplayableId);
                    }
                }
                catch (Exception ex)
                {
                    Utils.Trace.WriteLine(string.Format("User: {0}, Tenant: {1} {2}", cacheInfo.DisplayableId, tenantId, ex.Message));
                    Utils.Trace.WriteLine(string.Empty);
                    continue;
                }

                try
                {
                    TokenCacheInfo aadToken = null;
                    if (!String.IsNullOrEmpty(appId) && appKey == "_certificate_")
                    {
                        Utils.Trace.WriteLine(string.Format("AppId: {0}, Tenant: {1}", appId, tenantId));
                    }
                    else if (!String.IsNullOrEmpty(appId) && !String.IsNullOrEmpty(appKey))
                    {
                        aadToken = GetAuthorizationResultBySpn(tokenCache, tenantId: tenantId, appId: appId, appKey: appKey, resource: Constants.AADGraphUrls[(int)AzureEnvironments]);
                    }
                    else if (!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password))
                    {
                        aadToken = GetAuthorizationResultByUpn(tokenCache, tenantId: tenantId, username: username, password: password, resource: Constants.AADGraphUrls[(int)AzureEnvironments]);
                    }
                    else
                    {
                        aadToken = await GetAuthorizationResult(tokenCache, tenantId : tenantId, user : cacheInfo.DisplayableId, resource : Constants.AADGraphUrls[(int)AzureEnvironments]);
                    }

                    if (aadToken != null)
                    {
                        var details = await GetTenantDetail(aadToken, tenantId);

                        info.displayName = details.displayName;
                        info.domain      = details.verifiedDomains.First(d => d.@default).name;

                        if (!String.IsNullOrEmpty(appId) && !String.IsNullOrEmpty(appKey))
                        {
                            Utils.Trace.WriteLine(string.Format("AppId: {0}, Tenant: {1} ({2})", appId, tenantId, info.domain));
                        }
                        else
                        {
                            Utils.Trace.WriteLine(string.Format("User: {0}, Tenant: {1} ({2})", result.DisplayableId, tenantId, info.domain));
                        }
                    }
                }
                catch (Exception)
                {
                    if (!String.IsNullOrEmpty(appId) && !String.IsNullOrEmpty(appKey))
                    {
                        Utils.Trace.WriteLine(string.Format("AppId: {0}, Tenant: {1}", appId, tenantId));
                    }
                    else
                    {
                        Utils.Trace.WriteLine(string.Format("User: {0}, Tenant: {1}", result.DisplayableId, tenantId));
                    }
                }

                try
                {
                    var subscriptions = await GetSubscriptions(result);

                    Utils.Trace.WriteLine(string.Format("\tThere are {0} subscriptions", subscriptions.Length));

                    info.subscriptions = subscriptions.Select(subscription => new SubscriptionCacheInfo
                    {
                        subscriptionId = subscription.subscriptionId,
                        displayName    = subscription.displayName
                    }).ToArray();

                    if (recentInfo != null && info.subscriptions.Length > 0)
                    {
                        recentInfo = result;
                    }

                    foreach (var subscription in subscriptions)
                    {
                        Utils.Trace.WriteLine(string.Format("\tSubscription {0} ({1})", subscription.subscriptionId, subscription.displayName));
                    }
                }
                catch (Exception ex)
                {
                    Utils.Trace.WriteLine(string.Format("\t{0}!", ex.Message));
                }

                tenantCache[tenantId] = info;
                if (!String.IsNullOrEmpty(info.domain) && info.domain != "unknown")
                {
                    tenantCache[info.domain] = info;
                }

                Utils.Trace.WriteLine(string.Empty);
            }

            this.TokenStorage.SaveRecentToken(recentInfo, Constants.CSMResources[(int)AzureEnvironments]);

            return(tenantCache);
        }
Ejemplo n.º 3
0
        public async Task AzLogin()
        {
            this.TokenStorage.ClearCache();
            this.TenantStorage.ClearCache();

            var tokens = GetAzLoginTokens();

            var            tokenCache  = new CustomTokenCache();
            var            tenantCache = this.TenantStorage.GetCache();
            TokenCacheInfo recentInfo  = null;

            foreach (var token in tokens)
            {
                var  result = token.ToTokenCacheInfo();
                Guid unused;
                if (!Guid.TryParse(result.TenantId, out unused))
                {
                    continue;
                }

                tokenCache.Add(result);

                var tenantId = result.TenantId;
                var info     = new TenantCacheInfo
                {
                    tenantId    = tenantId,
                    displayName = "unknown",
                    domain      = tenantId
                };

                Utils.Trace.WriteLine(string.Format("User: {0}, Tenant: {1}", result.DisplayableId, tenantId));
                try
                {
                    var subscriptions = await GetSubscriptions(result);

                    Utils.Trace.WriteLine(string.Format("\tThere are {0} subscriptions", subscriptions.Length));

                    info.subscriptions = subscriptions.Select(subscription => new SubscriptionCacheInfo
                    {
                        subscriptionId = subscription.subscriptionId,
                        displayName    = subscription.displayName
                    }).ToArray();

                    if (recentInfo == null || info.subscriptions.Length > 0)
                    {
                        recentInfo = result;
                    }

                    foreach (var subscription in subscriptions)
                    {
                        Utils.Trace.WriteLine(string.Format("\tSubscription {0} ({1})", subscription.subscriptionId, subscription.displayName));
                    }
                }
                catch (Exception ex)
                {
                    Utils.Trace.WriteLine(string.Format("\t{0}!", ex.Message));
                }

                tenantCache[tenantId] = info;
                if (!String.IsNullOrEmpty(info.domain) && info.domain != "unknown")
                {
                    tenantCache[info.domain] = info;
                }

                Utils.Trace.WriteLine(string.Empty);
            }

            if (recentInfo != null)
            {
                this.TokenStorage.SaveRecentToken(recentInfo, Constants.CSMResources[(int)AzureEnvironments]);
            }

            this.TokenStorage.SaveCache(tokenCache);
            this.TenantStorage.SaveCache(tenantCache);
        }