示例#1
0
        private static void DownloadAdsGroupsMembership()
        {
            using (var context = new OnlineFilesEntities())
                using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
                    using (var groupPrincipal = new GroupPrincipalEx(ctx))
                        using (PrincipalSearcher search = new PrincipalSearcher(groupPrincipal))
                        {
                            int max = search.FindAll().Count();
                            int c   = 0;
                            foreach (var gp in search.FindAll().Select(found => found as GroupPrincipalEx))
                            {
                                Console.WriteLine("Processing " + c + " of " + max);
                                c++;
                                if (gp != null)
                                {
                                    if (gp.IsSecurityGroup != true && gp.GroupScope == GroupScope.Local)
                                    {
                                        continue;
                                    }
                                    var so = context.SecurityObjects.Include(d => d.MyGroups).FirstOrDefault(d => d.ActiveDirectoryId == gp.Guid);
                                    if (so == null)
                                    {
                                        throw new Exception("Not Possible");
                                    }

                                    context.SecurityObjectMemberships.RemoveRange(context.SecurityObjectMemberships.Where(d => d.SecurityObjectId == so.SecurityObjectId));
                                    context.SaveChanges();

                                    try
                                    {
                                        foreach (Principal grp in gp.GetGroups())
                                        {
                                            var og = context.SecurityObjects.FirstOrDefault(d => d.ActiveDirectoryId == grp.Guid);
                                            if (og != null)
                                            {
                                                context.SecurityObjectMemberships.Add(new SecurityObjectMembership {
                                                    OwnerSecurityObject = so, GroupSecurityObjectId = og.SecurityObjectId
                                                });
                                            }
                                        }
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }
                                context.SaveChanges();
                            }
                        }
        }
示例#2
0
 private static void DownloadAdsGroups()
 {
     using (var context = new OnlineFilesEntities())
         using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
             using (var groupPrincipal = new GroupPrincipalEx(ctx))
                 using (PrincipalSearcher search = new PrincipalSearcher(groupPrincipal))
                 {
                     int max = search.FindAll().Count();
                     int c   = 0;
                     foreach (var gp in search.FindAll().Select(found => found as GroupPrincipalEx))
                     {
                         Console.WriteLine("Processing " + c + " of " + max);
                         c++;
                         if (gp != null)
                         {
                             if (gp.IsSecurityGroup != true && gp.GroupScope == GroupScope.Local)
                             {
                                 continue;
                             }
                             var so = context.SecurityObjects.FirstOrDefault(d => d.ActiveDirectoryId == gp.Guid);
                             if (so == null)
                             {
                                 so = new SecurityObject
                                 {
                                     ActiveDirectoryId = gp.Guid,
                                     FullName          = gp.Name,
                                     Username          = gp.SamAccountName,
                                     EmailAddress      = gp.EmailAddress ?? "",
                                     IsGroup           = true,
                                     LastLogInOn       = DateTime.Now,
                                     IsActive          = true,
                                     HomeFolder        = null
                                 };
                                 context.SecurityObjects.Add(so);
                             }
                             else
                             {
                                 so.IsGroup      = true;
                                 so.FullName     = gp.Name;
                                 so.Username     = gp.SamAccountName;
                                 so.EmailAddress = gp.EmailAddress ?? "";
                             }
                         }
                         context.SaveChanges();
                     }
                 }
 }
 private static void DownloadAdsGroups()
 {
     using (var context = new OnlineFilesEntities())
     using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
     using (var groupPrincipal = new GroupPrincipalEx(ctx))
     using (PrincipalSearcher search = new PrincipalSearcher(groupPrincipal))
     {
         int max = search.FindAll().Count();
         int c = 0;
         foreach (var gp in search.FindAll().Select(found => found as GroupPrincipalEx))
         {
             Console.WriteLine("Processing " + c + " of " + max);
             c++;
             if (gp != null)
             {
                 if (gp.IsSecurityGroup != true && gp.GroupScope == GroupScope.Local)
                     continue;
                 var so = context.SecurityObjects.FirstOrDefault(d => d.ActiveDirectoryId == gp.Guid);
                 if (so == null)
                 {
                     so = new SecurityObject
                     {
                         ActiveDirectoryId = gp.Guid,
                         FullName = gp.Name,
                         Username = gp.SamAccountName,
                         EmailAddress = gp.EmailAddress ?? "",
                         IsGroup = true,
                         LastLogInOn = DateTime.Now,
                         IsActive = true,
                         HomeFolder = null
                     };
                     context.SecurityObjects.Add(so);
                 }
                 else
                 {
                     so.IsGroup = true;
                     so.FullName = gp.Name;
                     so.Username = gp.SamAccountName;
                     so.EmailAddress = gp.EmailAddress ?? "";
                 }
             }
             context.SaveChanges();
         }
     }
 }
        private static void DownloadAdsGroupsMembership()
        {
            using (var context = new OnlineFilesEntities())
            using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
            using (var groupPrincipal = new GroupPrincipalEx(ctx))
            using (PrincipalSearcher search = new PrincipalSearcher(groupPrincipal))
            {
                int max = search.FindAll().Count();
                int c = 0;
                foreach (var gp in search.FindAll().Select(found => found as GroupPrincipalEx))
                {
                    Console.WriteLine("Processing " + c + " of " + max);
                    c++;
                    if (gp != null)
                    {
                        if (gp.IsSecurityGroup != true && gp.GroupScope == GroupScope.Local)
                            continue;
                        var so = context.SecurityObjects.Include(d => d.MyGroups).FirstOrDefault(d => d.ActiveDirectoryId == gp.Guid);
                        if (so == null)
                            throw new Exception("Not Possible");

                        context.SecurityObjectMemberships.RemoveRange(context.SecurityObjectMemberships.Where(d => d.SecurityObjectId == so.SecurityObjectId));
                        context.SaveChanges();

                        try
                        {
                            foreach (Principal grp in gp.GetGroups())
                            {
                                var og = context.SecurityObjects.FirstOrDefault(d => d.ActiveDirectoryId == grp.Guid);
                                if (og != null)
                                    context.SecurityObjectMemberships.Add(new SecurityObjectMembership {OwnerSecurityObject = so, GroupSecurityObjectId = og.SecurityObjectId});
                            }
                        }
                        catch (Exception)
                        {
                        }
                    }
                    context.SaveChanges();
                }
            }
        }