Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Azure.ActiveDirectory.GraphClient;
- using Microsoft.IdentityModel.Clients.ActiveDirectory;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.Azure.ActiveDirectory.GraphClient.Extensions;
- namespace TestGraphAPI
- {
- class Program
- {
- private static readonly string AadInstance = "https://login.windows.net/common";
- public const string ClientId = "dca9afe0-a2cd-46a5-9dd1-d046f3b30676";
- private static readonly Uri RedirectUri = new Uri("https://azuredatacatalog.com/oauth/replyto");
- static void Main(string[] args)
- {
- bool spinAgain;
- do
- {
- var task = SpinIt().Result;
- Console.WriteLine();
- Console.Write("Spin again? (true/false): ");
- var input = Console.ReadLine();
- bool.TryParse(input, out spinAgain);
- Console.WriteLine();
- } while (spinAgain);
- Console.ReadKey();
- }
- static async Task<int> SpinIt()
- {
- try
- {
- var authContext = new AuthenticationContext(AadInstance);
- authContext.AcquireToken("https://api.azuredatacatalog.com", ClientId, RedirectUri, PromptBehavior.Auto);
- var result = authContext.AcquireToken("https://graph.windows.net", ClientId, RedirectUri, PromptBehavior.Never);
- var baseUri = new Uri("https://graph.windows.net/");
- var fullUri = new Uri(baseUri, result.TenantId);
- var upns = new[]
- {
- "satyan@microsoft.com", "@%#$T#%T#%", "ganesu@microsoft.com", "bobby@gmail.com", "selala@microsoft.com",
- "jamesfi@microsoft.com", "nadejdap@microsoft.com", "v-pareit@microsoft.com", "alexli@microsoft.com", "ingave@microsoft.com",
- "petergv@microsoft.com", "edgarse@microsoft.com", "haoc@microsoft.com", "linm@microsoft.com", "ankinra@microsoft.com",
- "abzaib@microsoft.com", "dkershaw@microsoft.com", "vbbala@microsoft.com", "ramraman@microsoft.com", "rokogan@microsoft.com",
- "samart@microsoft.com", "etha@microsoft.com", "svankam@microsoft.com", "evdegr@microsoft.com", "yurybog@microsoft.com",
- "nikindb@microsoft.com", "maxli@microsoft.com", "miown@microsoft.com", "jatillin@microsoft.com", "aaudu@microsoft.com",
- "ujwalk@microsoft.com", "minhdo@microsoft.com", "bla@microsoft.com", "bob@test.com"
- };
- var validUsers = new List<IUser>();
- var invalidUsers = new List<string>();
- var failedUsers = new List<string>();
- try
- {
- var tasks = upns.Select(upn => Task.Run(async () =>
- {
- try
- {
- var graphClient = new ActiveDirectoryClient(fullUri,
- () => Task.Run(() => result.AccessToken));
- IPagedCollection<IUser> pagedCollection = null;
- bool retry = false;
- try
- {
- pagedCollection = await
- graphClient.Users.Where(
- u => u.UserPrincipalName.Equals(upn, StringComparison.CurrentCultureIgnoreCase))
- .ExecuteAsync();
- }
- catch (Exception e)
- {
- //If you put a breakpoint in this catch, you'll see the exception.
- Console.WriteLine("Retrying for upn: {0}", upn);
- retry = true;
- }
- if (retry)
- {
- pagedCollection = await
- graphClient.Users.Where(
- u => u.UserPrincipalName.Equals(upn, StringComparison.CurrentCultureIgnoreCase))
- .ExecuteAsync();
- }
- if (pagedCollection == null || pagedCollection.CurrentPage == null ||
- pagedCollection.CurrentPage.Count == 0)
- {
- lock (invalidUsers)
- {
- Console.WriteLine("Invalid user: {0}", upn);
- invalidUsers.Add(upn);
- }
- }
- else
- {
- lock (validUsers)
- {
- Console.WriteLine("Valid user: {0}", upn);
- validUsers.Add(pagedCollection.CurrentPage.First());
- }
- }
- }
- catch (Exception e)
- {
- lock (failedUsers)
- {
- Console.WriteLine("Failed user: {0}", upn);
- failedUsers.Add(upn);
- }
- PrintException(e);
- }
- }));
- var succeeded = Task.WaitAll(tasks.ToArray(), 30000);
- Console.WriteLine();
- Console.WriteLine("Succeeded: {0}", succeeded);
- }
- catch (Exception e)
- {
- PrintException(e);
- }
- }
- catch (Exception ex)
- {
- PrintException(ex);
- }
- return 2;
- }
- static void PrintException(Exception ex)
- {
- if (ex.InnerException != null)
- {
- PrintException(ex.InnerException);
- }
- Console.WriteLine(ex.Message);
- if (ex is AggregateException)
- {
- var agg = ex as AggregateException;
- foreach (var exInner in agg.InnerExceptions)
- {
- PrintException(exInner);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement