public void getUsersGroupMembership(Excel.Worksheet Wks, string strUserName) { CommonExcelClasses.setCursorToWaiting(); try { myData = myData.LoadMyData(); // read data from settings file bool boolTestCode = myData.TestCode; // Connection information // var connectionString = "LDAP://domain.com/DC=domain,DC=com"; string connectionString = "LDAP://subsea7.net/DC=subsea7,DC=net"; // Split the LDAP Uri var uri = new Uri(connectionString); var host = uri.Host; var container = uri.Segments.Count() >= 1 ? uri.Segments[1] : ""; // Create context to connect to AD var princContext = new PrincipalContext(ContextType.Domain, host, container); // Get User UserPrincipal user = UserPrincipal.FindByIdentity(princContext, IdentityType.SamAccountName, strUserName); int intRow = 2; int intCol = 1; // Browse user's groups foreach (GroupPrincipal group in user.GetGroups()) { intCol = 1; Wks.Cells[intRow, intCol].Value = group.Name; intCol++; Wks.Cells[intRow, intCol].Value = group.Description; intCol++; Wks.Cells[intRow, intCol].Value = group.IsSecurityGroup; intCol++; Wks.Cells[intRow, intCol].Value = group.GroupScope.ToString(); intCol++; intRow++; Console.Out.WriteLine(group.Name); } } catch (Exception excpt) { CommonExcelClasses.MsgBox("There was a problem: " + excpt.Message + " was the variable a User?"); Console.WriteLine(excpt.Message); // throw; } CommonExcelClasses.setCursorToDefault(); }
public void getGroupUserMembership(Excel.Worksheet Wks, string strGroupName) { CommonExcelClasses.setCursorToWaiting(); try { myData = myData.LoadMyData(); // read data from settings file bool boolTestCode = myData.TestCode; // can I used the same code for both? // Connection information // var connectionString = "LDAP://domain.com/DC=domain,DC=com"; string connectionString = "LDAP://subsea7.net/DC=subsea7,DC=net"; // Split the LDAP Uri var uri = new Uri(connectionString); var host = uri.Host; var container = uri.Segments.Count() >= 1 ? uri.Segments[1] : ""; int intRow = 2; int intCol = 1; var princContext = new PrincipalContext(ContextType.Domain, host, container); if (boolTestCode) { // Create context to connect to AD // Get group GroupPrincipal qbeGroup = new GroupPrincipal(princContext, strGroupName); PrincipalSearcher srch = new PrincipalSearcher(qbeGroup); // find all matches foreach (var found in srch.FindAll()) { if (found is GroupPrincipal foundGroup) { // iterate over members foreach (Principal user in foundGroup.GetMembers()) { intCol = 1; Wks.Cells[intRow, intCol].Value = user.SamAccountName; intCol++; Wks.Cells[intRow, intCol].Value = user.DisplayName; intCol++; Wks.Cells[intRow, intCol].Value = user.Description; intCol++; intRow++; } } } } else { // PrincipalContext princContext = new PrincipalContext(ContextType.Domain); GroupPrincipal group = GroupPrincipal.FindByIdentity(princContext, strGroupName); if (group != null) { // iterate over members foreach (Principal p in group.GetMembers()) { Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName); // do whatever you need to do to those members if (p is UserPrincipal User) { intCol = 1; Wks.Cells[intRow, intCol].Value = User.SamAccountName; intCol++; Wks.Cells[intRow, intCol].Value = User.DisplayName; intCol++; Wks.Cells[intRow, intCol].Value = User.Description; intCol++; Wks.Cells[intRow, intCol].Value = User.IsAccountLockedOut(); intCol++; intRow++; } } } } } catch (Exception excpt) { CommonExcelClasses.MsgBox("There was a problem: " + excpt.Message + " was the variable a Group?"); Console.WriteLine(excpt.Message); throw; } CommonExcelClasses.setCursorToDefault(); }