Advertisement
Guest User

Untitled

a guest
Mar 30th, 2016
1,249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.64 KB | None | 0 0
  1. using Microsoft.Azure.ActiveDirectory.GraphClient;
  2. using Microsoft.IdentityModel.Clients.ActiveDirectory;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Microsoft.Azure.ActiveDirectory.GraphClient.Extensions;
  8.  
  9. namespace TestGraphAPI
  10. {
  11.     class Program
  12.     {
  13.         private static readonly string AadInstance = "https://login.windows.net/common";
  14.         public const string ClientId = "dca9afe0-a2cd-46a5-9dd1-d046f3b30676";
  15.         private static readonly Uri RedirectUri = new Uri("https://azuredatacatalog.com/oauth/replyto");
  16.  
  17.         static void Main(string[] args)
  18.         {
  19.             bool spinAgain;
  20.  
  21.             do
  22.             {
  23.                 var task = SpinIt().Result;
  24.  
  25.                 Console.WriteLine();
  26.                 Console.Write("Spin again? (true/false): ");
  27.                 var input = Console.ReadLine();
  28.  
  29.                 bool.TryParse(input, out spinAgain);
  30.  
  31.                 Console.WriteLine();
  32.             } while (spinAgain);
  33.  
  34.             Console.ReadKey();
  35.         }
  36.  
  37.         static async Task<int> SpinIt()
  38.         {
  39.             try
  40.             {
  41.                 var authContext = new AuthenticationContext(AadInstance);
  42.                 authContext.AcquireToken("https://api.azuredatacatalog.com", ClientId, RedirectUri, PromptBehavior.Auto);
  43.                 var result = authContext.AcquireToken("https://graph.windows.net", ClientId, RedirectUri, PromptBehavior.Never);
  44.  
  45.                 var baseUri = new Uri("https://graph.windows.net/");
  46.                 var fullUri = new Uri(baseUri, result.TenantId);
  47.  
  48.                 var upns = new[]
  49.                 {
  50.                     "satyan@microsoft.com", "@%#$T#%T#%", "ganesu@microsoft.com", "bobby@gmail.com", "selala@microsoft.com",
  51.                     "jamesfi@microsoft.com", "nadejdap@microsoft.com", "v-pareit@microsoft.com", "alexli@microsoft.com", "ingave@microsoft.com",
  52.                     "petergv@microsoft.com", "edgarse@microsoft.com", "haoc@microsoft.com", "linm@microsoft.com", "ankinra@microsoft.com",
  53.                     "abzaib@microsoft.com", "dkershaw@microsoft.com", "vbbala@microsoft.com", "ramraman@microsoft.com", "rokogan@microsoft.com",
  54.                     "samart@microsoft.com", "etha@microsoft.com", "svankam@microsoft.com", "evdegr@microsoft.com", "yurybog@microsoft.com",
  55.                     "nikindb@microsoft.com", "maxli@microsoft.com", "miown@microsoft.com", "jatillin@microsoft.com", "aaudu@microsoft.com",
  56.                     "ujwalk@microsoft.com", "minhdo@microsoft.com", "bla@microsoft.com", "bob@test.com"
  57.                 };
  58.  
  59.                 var validUsers = new List<IUser>();
  60.                 var invalidUsers = new List<string>();
  61.                 var failedUsers = new List<string>();
  62.  
  63.                 try
  64.                 {
  65.                     var tasks = upns.Select(upn => Task.Run(async () =>
  66.                     {
  67.                         try
  68.                         {
  69.                             var graphClient = new ActiveDirectoryClient(fullUri,
  70.                                 () => Task.Run(() => result.AccessToken));
  71.                             IPagedCollection<IUser> pagedCollection = null;
  72.                             bool retry = false;
  73.                             try
  74.                             {
  75.                                  pagedCollection = await
  76.                                     graphClient.Users.Where(
  77.                                         u => u.UserPrincipalName.Equals(upn, StringComparison.CurrentCultureIgnoreCase))
  78.                                         .ExecuteAsync();
  79.                             }
  80.                             catch (Exception e)
  81.                             {
  82.                                 //If you put a breakpoint in this catch, you'll see the exception.
  83.                                 Console.WriteLine("Retrying for upn: {0}", upn);
  84.                                 retry = true;
  85.                             }
  86.  
  87.                             if (retry)
  88.                             {
  89.                                 pagedCollection = await
  90.                                     graphClient.Users.Where(
  91.                                         u => u.UserPrincipalName.Equals(upn, StringComparison.CurrentCultureIgnoreCase))
  92.                                         .ExecuteAsync();
  93.                             }
  94.                            
  95.                             if (pagedCollection == null || pagedCollection.CurrentPage == null ||
  96.                                 pagedCollection.CurrentPage.Count == 0)
  97.                             {
  98.                                 lock (invalidUsers)
  99.                                 {
  100.                                     Console.WriteLine("Invalid user: {0}", upn);
  101.                                     invalidUsers.Add(upn);
  102.                                 }
  103.                             }
  104.                             else
  105.                             {
  106.                                 lock (validUsers)
  107.                                 {
  108.                                     Console.WriteLine("Valid user: {0}", upn);
  109.                                     validUsers.Add(pagedCollection.CurrentPage.First());
  110.                                 }
  111.                             }
  112.  
  113.                         }
  114.                         catch (Exception e)
  115.                         {
  116.                             lock (failedUsers)
  117.                             {
  118.                                 Console.WriteLine("Failed user: {0}", upn);
  119.                                 failedUsers.Add(upn);
  120.                             }
  121.                             PrintException(e);
  122.                         }
  123.                     }));
  124.  
  125.                     var succeeded = Task.WaitAll(tasks.ToArray(), 30000);
  126.  
  127.                     Console.WriteLine();
  128.                     Console.WriteLine("Succeeded: {0}", succeeded);
  129.                 }
  130.                 catch (Exception e)
  131.                 {
  132.                     PrintException(e);
  133.                 }
  134.             }
  135.             catch (Exception ex)
  136.             {
  137.                 PrintException(ex);
  138.             }
  139.             return 2;
  140.         }
  141.  
  142.         static void PrintException(Exception ex)
  143.         {
  144.             if (ex.InnerException != null)
  145.             {
  146.                 PrintException(ex.InnerException);
  147.             }
  148.             Console.WriteLine(ex.Message);
  149.             if (ex is AggregateException)
  150.             {
  151.                 var agg = ex as AggregateException;
  152.                 foreach (var exInner in agg.InnerExceptions)
  153.                 {
  154.                     PrintException(exInner);
  155.                 }
  156.             }
  157.         }
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement