예제 #1
0
        public static void DumpTokenCache()
        {
            var tokenCache  = TokenCache.GetCache();
            var tenantCache = TenantCache.GetCache();

            if (tokenCache.Count > 0)
            {
                foreach (var value in tokenCache.Values.ToArray())
                {
                    var authResult = AuthenticationResult.Deserialize(Encoding.UTF8.GetString(Convert.FromBase64String(value)));
                    var tenantId   = authResult.TenantId;

                    if (InfrastructureTenantIds.Contains(tenantId))
                    {
                        continue;
                    }

                    var user    = authResult.UserInfo.UserId;
                    var details = tenantCache[tenantId];
                    Console.WriteLine("User: {0}, Tenant: {1} {2} ({3})", user, tenantId, details.displayName, details.domain);

                    var subscriptions = details.subscriptions;
                    Console.WriteLine("\tThere are {0} subscriptions", subscriptions.Length);

                    foreach (var subscription in subscriptions)
                    {
                        Console.WriteLine("\tSubscription {0} ({1})", subscription.subscriptionId, subscription.displayName);
                    }
                    Console.WriteLine();
                }
            }
        }
예제 #2
0
        public static async Task <AuthenticationResult> GetTokenBySubscription(string subscriptionId)
        {
            var tenantCache = TenantCache.GetCache();
            var pairs       = tenantCache.Where(p => p.Value.subscriptions.Any(subscription => subscriptionId == subscription.subscriptionId)).ToArray();

            if (pairs.Length == 0)
            {
                Console.WriteLine();
                Console.WriteLine(String.Format("Cannot find subscription {0} cache!   Use recent token instead.", subscriptionId));
                Console.WriteLine();

                return(await GetRecentToken());
            }

            return(await GetTokenByTenant(pairs[0].Key));
        }
예제 #3
0
        private static async Task <IDictionary <string, AuthenticationResult> > GetTokenForTenants(Dictionary <TokenCacheKey, string> tokenCache, AuthenticationResult authResult)
        {
            var tenantIds = await GetTenantIds(authResult);

            Console.WriteLine("User belongs to {1} tenants", authResult.UserInfo.UserId, tenantIds.Length);

            var tenantCache = TenantCache.GetCache();
            var results     = new Dictionary <string, AuthenticationResult>();

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

                AuthenticationResult result = null;
                try
                {
                    result = await GetAuthorizationResult(tokenCache, tenantId : tenantId, user : authResult.UserInfo.UserId);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("User: {0}, Tenant: {1} {2}", authResult.UserInfo.UserId, tenantId, ex.Message);
                    Console.WriteLine();
                    continue;
                }

                results[tenantId] = result;
                try
                {
                    var details = await GetTenantDetail(result, tenantId);

                    info.displayName = details.displayName;
                    info.domain      = details.verifiedDomains.First(d => d.@default).name;
                    Console.WriteLine("User: {0}, Tenant: {1} {2} ({3})", result.UserInfo.UserId, tenantId, details.displayName, details.verifiedDomains.First(d => d.@default).name);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("User: {0}, Tenant: {1} {2}", result.UserInfo.UserId, tenantId, ex.Message);
                }

                try
                {
                    var subscriptions = await GetSubscriptions(result);

                    Console.WriteLine("\tThere are {0} subscriptions", subscriptions.Length);

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

                    foreach (var subscription in subscriptions)
                    {
                        Console.WriteLine("\tSubscription {0} ({1})", subscription.subscriptionId, subscription.displayName);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("\t{0}!", ex.Message);
                }
                tenantCache[tenantId] = info;
                Console.WriteLine();
            }

            TenantCache.SaveCache(tenantCache);

            return(results);
        }
예제 #4
0
        public static async Task <AuthenticationResult> GetTokenByTenant(string tenantId)
        {
            bool found       = false;
            var  tenantCache = TenantCache.GetCache();

            if (tenantCache.ContainsKey(tenantId))
            {
                found = true;
            }

            if (!found)
            {
                foreach (var tenant in tenantCache)
                {
                    if (tenant.Value.subscriptions.Any(s => s.subscriptionId == tenantId))
                    {
                        tenantId = tenant.Key;
                        found    = true;
                        break;
                    }
                }
            }

            if (!found)
            {
                throw new InvalidOperationException(String.Format("Cannot find tenant {0} in cache!", tenantId));
            }

            var tokenCache  = TokenCache.GetCache();
            var authResults = tokenCache.Where(p => p.Key.TenantId == tenantId)
                              .Select(p => AuthenticationResult.Deserialize(Encoding.UTF8.GetString(Convert.FromBase64String(p.Value)))).ToArray();

            if (authResults.Length <= 0)
            {
                throw new InvalidOperationException(String.Format("Cannot find tenant {0} in cache!", tenantId));
            }

            if (authResults.Length > 1)
            {
                foreach (var authResult in authResults)
                {
                    Console.WriteLine(authResult.UserInfo.UserId);
                }

                throw new InvalidOperationException("Multiple users found.  Please specify user argument!");
            }
            else
            {
                var authResult = authResults[0];
                if (authResult.ExpiresOn <= DateTime.UtcNow)
                {
                    authResult = await GetAuthorizationResult(tokenCache, authResult.TenantId, authResult.UserInfo.UserId);

                    TokenCache.SaveCache(tokenCache);
                }

                SaveRecentToken(authResult);

                return(authResult);
            }
        }