当前位置: 首页>>代码示例>>C#>>正文


C# UserPrincipal.GetGroups方法代码示例

本文整理汇总了C#中System.DirectoryServices.AccountManagement.UserPrincipal.GetGroups方法的典型用法代码示例。如果您正苦于以下问题:C# UserPrincipal.GetGroups方法的具体用法?C# UserPrincipal.GetGroups怎么用?C# UserPrincipal.GetGroups使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.DirectoryServices.AccountManagement.UserPrincipal的用法示例。


在下文中一共展示了UserPrincipal.GetGroups方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateUserPrincipal

        public bool CreateUserPrincipal()
        {
            // Create connection to domain and do a search for the user
            try
            {
                context = new PrincipalContext(ContextType.Domain, givenDomain);

                    UserPrincipal tempUserPrincipal = new UserPrincipal(context);
                    tempUserPrincipal.SamAccountName = givenUserName;

                    // Search for user
                    PrincipalSearcher searchUser = new PrincipalSearcher();
                    searchUser.QueryFilter = tempUserPrincipal;

                    UserPrincipal foundUser = (UserPrincipal)searchUser.FindOne();

                    userPrincipal = foundUser;
                    userGroups = userPrincipal.GetGroups();
                    return true;

            }
            catch (PrincipalServerDownException)
            {
                System.Windows.Forms.MessageBox.Show("Cannot contact the server.");
                return false;
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message, "Unknown Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return false;
            }
        }
开发者ID:rtjust,项目名称:PastProjects,代码行数:32,代码来源:WinUserInfo.cs

示例2: UpdateUser

 protected void UpdateUser(UserPrincipal up, UserInfo user)
 {
     user.Name = up.DisplayName;
     user.Email = up.EmailAddress;
     user.Active = true;
     foreach (var g in up.GetGroups())
     {
         user.MemberOf.Add(g.SamAccountName);
     }
     user.ExtId = up.DistinguishedName;
 }
开发者ID:lafar6502,项目名称:cogmon,代码行数:11,代码来源:WindowsAuth.cs

示例3: AdUser

        /// <summary>
        /// Constructor that instantiates an AdUser object with the values of the specified
        /// UserPrincipal's attributes.
        /// </summary>
        /// <param name="user"></param>
        public AdUser(UserPrincipal user)
        {
            this.AccountExpirationData = user.AccountExpirationDate.ToString();
            this.AccountLockoutTime = user.AccountLockoutTime.ToString();
            this.AllowReversiblePasswordEncryption = user.AllowReversiblePasswordEncryption;
            this.BadLogonCount = user.BadLogonCount;
            this.Company = GetCompany(user);
            this.DelegationPermitted = user.DelegationPermitted;
            this.Department = GetDepartment(user);
            this.Description = user.Description;
            this.DisplayName = user.DisplayName;
            this.DistinguishedName = user.DistinguishedName;
            this.EmailAddress = user.EmailAddress;
            this.EmployeeId = user.EmployeeId;
            this.Enabled = user.Enabled;
            this.GivenName = user.GivenName;
            this.Guid = user.Guid.ToString();
            this.HomeDirectory = user.HomeDirectory;
            this.HomeDrive = user.HomeDrive;
            this.LastBadPasswordAttempt = user.LastBadPasswordAttempt.ToString();
            this.LastLogon = user.LastLogon.ToString();
            this.LastPasswordSet = user.LastPasswordSet.ToString();
            this.Manager = GetManager(user);
            this.MemberOf = AdGroup.GetAdGroupSamAccountNames(user.GetGroups());
            this.MiddleName = user.MiddleName;
            this.Mobile = GetMobile(user);
            this.Name = user.Name;
            this.PasswordNeverExpires = user.PasswordNeverExpires;
            this.PasswordNotRequired = user.PasswordNotRequired;
            this.ProfilePath = AdToolkit.GetProperty(user, "profilePath");
            this.SamAccountName = user.SamAccountName;
            this.ScriptPath = user.ScriptPath;
            this.Sid = user.Sid.Value;
            this.SmartCardLogonRequired = user.SmartcardLogonRequired;
            this.Surname = user.Surname;
            this.UserCannotChangePassword = user.UserCannotChangePassword;
            this.UserPrincipalName = user.UserPrincipalName;
            this.VoiceTelephoneNumber = user.VoiceTelephoneNumber;

            //user.GetGroups
        }
开发者ID:UAResLife,项目名称:AdApiService,代码行数:46,代码来源:AdUser.cs

示例4: ADUser

        /// <summary>
        /// Create an AD User from the given connection and UserPrincipal
        /// </summary>
        /// <param name="connection">The AD connection</param>
        /// <param name="user">An existing UserPrincipal object</param>
        public ADUser(ActiveDirectory connection, UserPrincipal user)
        {
            if (connection == null || user == null)
            {
                throw new NullReferenceException();
            }

            _sourceUser = user;
            _connection = connection;
            mapper = new DEMapper();
            DEFields.Add("manager");
            this.UserState = UserStates.Existing;

            foreach (System.Reflection.PropertyInfo property in this.GetType().GetProperties())
            {
                DEFieldAttribute tag= property.GetCustomAttributes(typeof(DEFieldAttribute),true).FirstOrDefault() as DEFieldAttribute;

                if (tag != null)
                {
                    DEFields.Add(tag.Name!=null?tag.Name:property.Name);
                }
            }

            if (user.Name != null)
            {
                _groups = new List<string>(_sourceUser.GetGroups(connection.GlobalContext).Select(item => item.Name));
            }

            this.Initialize(_sourceUser);

            if (_sourceUser.DisplayName != null)
            {
                directoryEntry = _sourceUser.GetUnderlyingObject() as DirectoryEntry;
                directoryEntry.RefreshCache(DEFields.ToArray<string>());
                mapper.Copy(directoryEntry, this);
            }

            string ldap=String.Format("LDAP://{0}/{1}", connection.Name, connection.Container);
            parent = new DirectoryEntry(ldap, connection.User, connection.Password);
        }
开发者ID:bolenc,项目名称:Active-Directory-Examples,代码行数:45,代码来源:ADUser.cs

示例5: GetWindowsGroupsUsingAD

        bool _tryAgainAD = true; //Try again to get rid of Load Assemblies exceptions...
        List<string> GetWindowsGroupsUsingAD(string contextType)
        {
            var result = new List<string>();
            try
            {
                // set up domain context
                PrincipalContext context = new PrincipalContext((ContextType)Enum.Parse(typeof(ContextType), contextType));
                string name = WebUserName;
                if (WebPrincipal != null) name = WebPrincipal.Identity.Name;
                if (Identity != null) name = Identity.Name;

                var user = new UserPrincipal(context);
                user.SamAccountName = name;
                var searcher = new PrincipalSearcher(user);
                user = searcher.FindOne() as UserPrincipal;
                if (user == null) user = UserPrincipal.FindByIdentity(context, name);

                if (user != null)
                {
                    // find the roles....
                    var roles = user.GetGroups();
                    // enumerate over them
                    foreach (Principal p in roles)
                    {
                        result.Add(p.Name);
                    }
                }
                else throw new Exception("Unable to find user:" + name);
            }
            catch (Exception ex)
            {
                if (_tryAgainAD)
                {
                    _tryAgainAD = false;
                    result = GetWindowsGroupsUsingAD(contextType);
                }
                else
                {
                    Warning += "Error getting groups using AD...\r\n" + ex.Message + "\r\n";
                }
            }
            return result;
        }
开发者ID:cnark,项目名称:Seal-Report,代码行数:44,代码来源:SecurityUser.cs

示例6: CreateLdapAccount

        public void CreateLdapAccount(LdapConfig cfg, bool allowPasswordChange)
        {
            string pwd = ldapContext.LdapConfigs.Decryptpassword(cfg);
            DirectoryEntry ou = new DirectoryEntry("LDAP://" + OuAssignment.OuDistinguishedName, cfg.UserName, pwd);

            string fn = Regex.Replace(Member.GivenName, @"[^A-Za-z0-9]+", "");
            string mn = null;
            if(Member.MiddleName != null)
                mn = Regex.Replace(Member.MiddleName, @"[^A-Za-z0-9]+", "");
            string ln = Regex.Replace(Member.Surname, @"[^A-Za-z0-9]+", "");




            string name = Lcps.DivisionDirectory.Members.DirectoryMemberRepository.GetName(DirectoryMemberNameFormats.Full | DirectoryMemberNameFormats.Sort, ln, fn, mn);
            string dn = string.Format("CN={0} ({1})", name.Replace(",", "\\,"), Member.UserName);

            var principalContext = new PrincipalContext(ContextType.Domain, cfg.DomainPrincipalName, cfg.UserName, pwd);


            string memberPw = Member.DecryptPassword();

            bool enabled = false;
            if ((Member.MembershipScope & Convert.ToInt64(MembershipScopeReserved.Active)) == Convert.ToInt64(MembershipScopeReserved.Active))
                enabled = true;

            if ((Member.MembershipScope & Convert.ToInt64(MembershipScopeReserved.Inactive)) == Convert.ToInt64(MembershipScopeReserved.Inactive))
                enabled = false;


            LdapUser = new UserPrincipal(principalContext, Member.UserName, memberPw, enabled);

            string scope = this.dirContext.MembershipScopes.GetCaptionLabel(Member.MembershipScope);
            LdapUser.Description = scope;
            LdapUser.DisplayName = Lcps.DivisionDirectory.Members.DirectoryMemberRepository.GetName(Member, DirectoryMemberNameFormats.Short | DirectoryMemberNameFormats.Sort);
            LdapUser.UserCannotChangePassword = (!allowPasswordChange);
            LdapUser.Surname = Member.Surname;
            LdapUser.GivenName = Member.GivenName;
            LdapUser.UserPrincipalName = Member.UserName + "@" + cfg.DomainPrincipalName;
            LdapUser.PasswordNeverExpires = true;
            LdapUser.EmployeeId = Member.InternalId;
            LdapUser.EmailAddress = Member.Email;

            try
            {
                LdapUser.Save();
            }
            catch (Exception ex)
            {
                throw new Exception("Could not create user", ex);
            }

            ou.RefreshCache();

            DirectoryEntry de = null;

            try
            {
                de = (DirectoryEntry)LdapUser.GetUnderlyingObject();
                de.InvokeSet("division", pwd);
                de.InvokeSet("comment", DateTime.Now.ToString());
                de.MoveTo(ou, dn);
                de.CommitChanges();
                de.RefreshCache();
                ou.RefreshCache();

                LdapUser = UserPrincipal.FindByIdentity(PrincipalContext, IdentityType.SamAccountName, Member.UserName);

            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Could not move user {0} to OU", dn), ex);
            }



            SyncGroupMemberships(false);

            bool gErr = true;
            int x = 1;

            while(gErr == true)
            {
                try
                {
                    x++;

                    DirectoryEntry thisU = (DirectoryEntry)LdapUser.GetUnderlyingObject();
                    thisU.CommitChanges();
                    ou.RefreshCache();
                    thisU.RefreshCache();
                    

                    foreach(GroupPrincipal g in LdapUser.GetGroups())
                    {
                        string n = g.Name;
                    }
                    gErr = false;
                }
                catch
//.........这里部分代码省略.........
开发者ID:themodulator,项目名称:Lcps.Repo.01,代码行数:101,代码来源:LdapUserConfig.cs

示例7: GetGroupsWithSupergroupsForUser

        private IEnumerable<DomainGroup> GetGroupsWithSupergroupsForUser(UserPrincipal userPrincipal)
        {
            var directGroups = userPrincipal.GetGroups();

            var supergroups = GetSupergroups(userPrincipal.GetGroups());
            var groups = directGroups.Select(ToDomainGroup).ToList();

            groups.AddRange(supergroups);

            return groups.Distinct().ToList();
        }
开发者ID:pashaiva,项目名称:psub.Web,代码行数:11,代码来源:DomainUserService.cs

示例8: GetGroupsForUser

 private IEnumerable<DomainGroup> GetGroupsForUser(UserPrincipal userPrincipal)
 {
     var groups = new List<DomainGroup>();
     var principalGroups = userPrincipal.GetGroups();
     foreach (var principalGroup in principalGroups)
     {
         groups.Add(ToDomainGroup(principalGroup));
     }
     return groups.Distinct().ToList();
 }
开发者ID:pashaiva,项目名称:psub.Web,代码行数:10,代码来源:DomainUserService.cs


注:本文中的System.DirectoryServices.AccountManagement.UserPrincipal.GetGroups方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。