internal AzureTenant CopyPersistedProperties() { AzureTenant copy = new AzureTenant() { TenantName = this.TenantName, TenantId = this.TenantId, ClientId = this.ClientId, ClientSecret = this.ClientSecret, }; return(copy); }
internal AzureTenant CopyPersistedProperties() { AzureTenant copy = new AzureTenant() { TenantName = this.TenantName, TenantId = this.TenantId, ClientId = this.ClientId, ClientSecret = this.ClientSecret, }; return copy; }
/// <summary> /// Query a specific Azure tenant /// </summary> /// <param name="userFilter"></param> /// <param name="groupFilter"></param> /// <param name="coco"></param> /// <returns></returns> private List<AzurecpResult> QueryAzureAD(AzureTenant coco, Expression<Func<IUser, bool>> userQuery, Expression<Func<IGroup, bool>> groupQuery) { using (new SPMonitoredScope(String.Format("[{0}] Connecting to Azure AD {1}", ProviderInternalName, coco.TenantName), 1000)) { if (coco.ADClient == null) { ActiveDirectoryClient activeDirectoryClient; try { activeDirectoryClient = AuthenticationHelper.GetActiveDirectoryClientAsApplication(coco.TenantName, coco.TenantId, coco.ClientId, coco.ClientSecret); } catch (AuthenticationException ex) { //You should implement retry and back-off logic per the guidance given here:http://msdn.microsoft.com/en-us/library/dn168916.aspx //InnerException Message will contain the HTTP error status codes mentioned in the link above AzureCPLogging.LogException(ProviderInternalName, String.Format("while acquiring token for tenant {0}", coco.TenantName), AzureCPLogging.Categories.Lookup, ex); return null; } coco.ADClient = activeDirectoryClient; AzureCPLogging.Log(String.Format("[{0}] Got new access token for tenant '{1}'", ProviderInternalName, coco.TenantName), TraceSeverity.Medium, EventSeverity.Information, AzureCPLogging.Categories.Lookup); //activeDirectoryClient.Oauth2PermissionGrants. //activeDirectoryClient.Oauth2PermissionGrants.Context. } List<AzurecpResult> allADResults = new List<AzurecpResult>(); // Workaroud implemented to avoid deadlock when calling DataServiceContextWrapper.ExecuteBatchAsync if (userQuery != null) { IUserCollection userCollection = coco.ADClient.Users; IPagedCollection<IUser> userSearchResults = null; do { userSearchResults = userCollection.Where(userQuery).ExecuteAsync().Result; List<IUser> searchResultsList = userSearchResults.CurrentPage.ToList(); foreach (IDirectoryObject objectResult in searchResultsList) { AzurecpResult azurecpResult = new AzurecpResult(); azurecpResult.DirectoryObjectResult = objectResult as DirectoryObject; azurecpResult.TenantId = coco.TenantId; allADResults.Add(azurecpResult); } userSearchResults = userSearchResults.GetNextPageAsync().Result; } while (userSearchResults != null && userSearchResults.MorePagesAvailable); } if (groupQuery != null) { IGroupCollection groupCollection = coco.ADClient.Groups; IPagedCollection<IGroup> groupSearchResults = null; do { groupSearchResults = groupCollection.Where(groupQuery).ExecuteAsync().Result; List<IGroup> searchResultsList = groupSearchResults.CurrentPage.ToList(); foreach (IDirectoryObject objectResult in searchResultsList) { AzurecpResult azurecpResult = new AzurecpResult(); azurecpResult.DirectoryObjectResult = objectResult as DirectoryObject; azurecpResult.TenantId = coco.TenantId; allADResults.Add(azurecpResult); } groupSearchResults = groupSearchResults.GetNextPageAsync().Result; } while (groupSearchResults != null && groupSearchResults.MorePagesAvailable); } return allADResults; } }
/// <summary> /// Gets the group membership of a user /// </summary> /// <param name="userToAugment"></param> /// <param name="coco"></param> /// <returns></returns> private List<AzurecpResult> GetUserMembership(User userToAugment, AzureTenant coco) { List<AzurecpResult> searchResults = new List<AzurecpResult>(); IUserFetcher retrievedUserFetcher = userToAugment; IPagedCollection<IDirectoryObject> pagedCollection = retrievedUserFetcher.MemberOf.ExecuteAsync().Result; do { List<IDirectoryObject> directoryObjects = pagedCollection.CurrentPage.ToList(); foreach (IDirectoryObject directoryObject in directoryObjects) { if (directoryObject is Group) { AzurecpResult result = new AzurecpResult(); Group group = directoryObject as Group; result.DirectoryObjectResult = group; result.TenantId = coco.TenantId; searchResults.Add(result); } //if (directoryObject is DirectoryRole) //{ // DirectoryRole role = directoryObject as DirectoryRole; //} } pagedCollection = pagedCollection.GetNextPageAsync().Result; } while (pagedCollection != null && pagedCollection.MorePagesAvailable); return searchResults; }