public JsonResult Post([FromBody] GraphAuth GraphAuth) { try { string host = "https://login.microsoftonline.com/"; string sufix = "/oauth2/v2.0/token"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(host + GraphAuth.TenatID + sufix); httpWebRequest.ContentType = "application/x-www-form-urlencoded"; httpWebRequest.Method = "POST"; var dataBody = "&grant_type=" + GraphAuth.GrantType; dataBody += "&client_id=" + GraphAuth.ClientID; dataBody += "&client_secret=" + GraphAuth.ClientSecret; dataBody += "&scope=" + GraphAuth.Scope; var dados = Encoding.UTF8.GetBytes(dataBody); using (var stream = httpWebRequest.GetRequestStream()) { stream.Write(dados, 0, dados.Length); stream.Close(); } using (var response = httpWebRequest.GetResponse()) { var streamDados = response.GetResponseStream(); StreamReader reader = new StreamReader(streamDados); object objResponse = reader.ReadToEnd(); streamDados.Close(); response.Close(); return(Json(JsonConvert.DeserializeObject(objResponse.ToString()))); } } catch (HttpListenerException ex) { return(Json(ex.Message)); } }
static void Main() { CancellationToken token = CancellationToken.None; GraphAuth graphAuth = new GraphAuth("btcloud1"); AuthenticationResult result = null; try { result = graphAuth.GetAuthResult(); } catch (Exception e) { Trace.WriteLine(e.Message); } if (result != null) { } O365Graph graph = new O365Graph(graphAuth); int orgCount = 0; foreach (var orgs in graph.GetOrganizations(token)) { orgCount += orgs.Count; } Trace.WriteLine($"Organization count: {orgCount}"); int userCount = 0; foreach (var users in graph.GetUsers(token)) { userCount += users.Count; } Trace.WriteLine($"User count: {userCount}"); int dlCount = 0; foreach (var distributionGroups in graph.GetDistributionGroups(token)) { dlCount += distributionGroups.Count; } Trace.WriteLine($"DistributionGroup count: {dlCount}"); int groupCount = 0; foreach (List <Group> groups in graph.GetGroups(token)) { groupCount += groups.Count; } Trace.WriteLine($"Group count: {groupCount}"); int securityCount = 0; foreach (var securityGroups in graph.GetMailEnabledSecurityGroups(token)) { securityCount += securityGroups.Count; } Trace.WriteLine($"SecurityGroup count: {securityCount}"); int unifiedCount = 0; foreach (var specialGroups in graph.GetSpecialGroups("Unified", token)) { unifiedCount += specialGroups.Count; } Trace.WriteLine($"UnifiedGroup count: {unifiedCount}"); int probeUserCount = 0; foreach (var probeUsers in graph.FindUsersStartsWith("DisplayName", "MailProbe", token)) { probeUserCount += probeUsers.Count; } Trace.WriteLine($"ProbeUserCount count: {probeUserCount}"); }
private void WaitForAddedItems(WaitInfo source, WaitInfo target, TimeSpan waitSpan, CancellationToken token) { Stopwatch swElapsed = Stopwatch.StartNew(); Log.Info("initializing GraphAuth for source"); GraphAuth graphAuthSource = new GraphAuth(source.TenantId, source.AppId, source.User, source.Pwd); Log.Info("initializing O365Graph for source"); O365Graph graphSource = new O365Graph(graphAuthSource); Log.Info("initializing GraphAuth for target"); GraphAuth graphAuthTarget = new GraphAuth(target.TenantId, target.AppId, target.User, target.Pwd); Log.Info("initializing O365Graph for target"); O365Graph graphTarget = new O365Graph(graphAuthTarget); User sourceUser = null; bool foundTarget = false; Group sourceGroup = null; bool isGroupMember = false; while (!token.IsCancellationRequested) { try { if (swElapsed.Elapsed >= waitSpan) { throw new Exception("Timed out waiting for user"); } if (sourceUser == null) { foreach (List <User> probeUsers in graphSource.FindUsersStartsWith("DisplayName", Store.ProbeSourceMailbox, token)) { sourceUser = probeUsers.FirstOrDefault(); break; } } if (sourceGroup == null) { sourceGroup = graphSource.FindGroupByDisplayNameOrMail(source.Group, token).Result; } if (!isGroupMember) { if (sourceUser != null && sourceGroup != null) { // now see if the source user is in the group foreach (List <DirectoryObject> members in graphSource.GetGroupMembers(sourceGroup, token)) { var foo = members.FirstOrDefault(x => x.Id == sourceUser.Id); isGroupMember = foo != null; if (isGroupMember) { break; } } } } if (!foundTarget) { foreach (List <User> probeUsers in graphTarget.FindUsersStartsWith("DisplayName", Store.ProbeTargetMailbox, token)) { foundTarget = true; break; } } if (foundTarget && isGroupMember) { Log.Info($"Wait elapsed: {swElapsed.Elapsed}"); return; } Task.Delay(TimeSpan.FromSeconds(20), token).Wait(token); } catch (Exception ex) { Log.Info($"Source user is: {sourceUser}"); Log.Info($"Source group is: {sourceGroup}"); Log.Info($"isGroupMember is: {isGroupMember}"); Log.Info($"FoundTarget is: {foundTarget}"); Log.Info($"Elapsed time: {swElapsed.Elapsed}"); if (swElapsed.Elapsed >= waitSpan) { throw; } Log.Error("Wait exception", ex); } } }