Exemple #1
0
        public bool LoadDomainUser(string domainName, string domainUpn)
        {
            log.WriteLogEntry("Begin LoadUser...");
            bool       result = false;
            DomainUser user   = new DomainUser();

            log.WriteLogEntry(string.Format("Domain name {0} Domain UPN {1}", domainName, domainUpn));
            using (UserContext = new PrincipalContext(ContextType.Domain, domainName))
            {
                UserAccount = new UserPrincipal(UserContext)
                {
                    UserPrincipalName = domainUpn
                };
                using (PrincipalSearcher UserSearch = new PrincipalSearcher())
                {
                    UserSearch.QueryFilter = UserAccount;
                    using (PrincipalSearchResult <Principal> Psr = UserSearch.FindAll())
                    {
                        UserAccount         = (UserPrincipal)Psr.First <Principal>();
                        user.FirstName      = UserAccount.GivenName;
                        user.LastName       = UserAccount.Surname;
                        user.DomainUserName = UserAccount.SamAccountName;
                        user.DomainUpn      = UserAccount.UserPrincipalName;
                        user.UserEmail      = UserAccount.EmailAddress;
                        user.EmployeeID     = UserAccount.EmployeeId;
                        this.CurrentUser    = user;
                        result = true;
                    }
                }
            }
            log.WriteLogEntry("End LoadUser.");
            return(result);
        }
        /// <summary>
        ///  Makes user with given login name member of group with given name if exists.
        /// </summary>
        /// <param name="groupName">group name</param>
        public void AssignUserGroup(string loginName, string groupName)
        {
            // Find user entry by login name
            UserPrincipal userEntry = UserPrincipal.FindByIdentity(activeDirectoryDomain,
                                                                   IdentityType.SamAccountName,
                                                                   loginName);

            // Search for group with matching name
            PrincipalSearchResult <Principal> activeDirectoryGroups = this.ListGroupsByName(
                this.activeDirectoryDomain, groupName);

            if (activeDirectoryGroups.Count <Principal>() != 0)
            {
                GroupPrincipal group = (GroupPrincipal)activeDirectoryGroups.First <Principal>();

                // make user member of group
                group.Members.Add(userEntry);

                // save changes
                group.Save();
                group.Dispose();
            }
            else
            {
                // throw exception to notify the group does not exists
                throw new ApplicationException("Domain group not found.");
            }

            // dispose the objects
            userEntry.Dispose();
            activeDirectoryGroups.Dispose();
        }
Exemple #3
0
        public static bool CheckGroupMembership(string userID, string groupName, string Domain)
        {
//#if TRACE
//            long startTicks = VNC.AppLog.Trace5("Start", LOG_APPNAME);
//#endif

            bool isMember = false;

            PrincipalSearchResult <Principal> groups = GetAuthorizationGroupsMembership(userID, Domain);

//#if TRACE
//            VNC.AppLog.Trace5("After GetAuthorizationGroupsMembership", LOG_APPNAME, startTicks);
//#endif


//#if TRACE
//            VNC.AppLog.Trace5(string.Format("After GetAuthorizationGroupsMembership {0}", groups.Count()), LOG_APPNAME, startTicks);
//#endif
            Principal foo = groups.First(g => g.Name == groupName);

//#if TRACE
//            VNC.AppLog.Trace5("After First", LOG_APPNAME, startTicks);
//#endif

            if (foo != null)
            {
                isMember = true;
            }
            int count = groups.Where(g => g.Name == groupName).Count();

//#if TRACE
//            VNC.AppLog.Trace5(string.Format("After Where {0}", count), LOG_APPNAME, startTicks);
//#endif

            using (PrincipalContext ADDomain = new PrincipalContext(ContextType.Domain, Domain))
            {
//#if TRACE
//                VNC.AppLog.Trace5("After new Principal", LOG_APPNAME, startTicks);
//#endif

                using (UserPrincipal user = UserPrincipal.FindByIdentity(ADDomain, userID))
                {
//#if TRACE
//                    VNC.AppLog.Trace5("After FindByIdentity", LOG_APPNAME, startTicks);
//#endif

                    if (count > 0)
                    {
                        isMember = true;
                    }
                }
            }

//#if TRACE
//            VNC.AppLog.Trace5("End", LOG_APPNAME, startTicks);
//#endif

            return(isMember);
        }
Exemple #4
0
 /// <summary>
 /// Gets the Guid for user from Active Directory
 /// </summary>
 /// <param name="username">The user's username</param>
 /// <returns>The GUID from Active Directory</returns>
 public Guid GetGuid(string username)
 {
     using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, ACTIVE_DIRECTORY_DOMAIN))
     {
         GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, ACTIVE_DIRECTORY_GROUP);
         PrincipalSearchResult <Principal> groupusers = group.GetMembers();
         return(groupusers.First(u => (u as UserPrincipal).SamAccountName.ToLower() == username.ToLower()).Guid.Value);
     }
 }