Beispiel #1
0
        public static commonUserFields GetCommonUserFields(string domainName, string userName, NetworkCredential cred)
        {
            LdapConnection connection = new LdapConnection(domainName);

            connection.Credential = cred;
            string dn       = GetDomainDnFromDomainName(domainName);
            string targetOu = "cn=Users," + dn;
            string filter   = "cn=" + userName;

            string[]         attributesToReturn = new string[] { "lastLogon", "logonHours", "accountExpires", "pwdLastSet", "dBCSPwd", "unicodePwd", "userAccountControl", "logonCount", "badPwdCount", "objectSid", "primaryGroupID", "memberOf" };
            commonUserFields userFields         = new commonUserFields();

            SearchRequest searchRequest = new SearchRequest(targetOu, filter, SearchScope.Subtree, attributesToReturn);

            SearchResponse searchResponse = (SearchResponse)connection.SendRequest(searchRequest);

            if (searchResponse.Entries.Count > 1)
            {
                throw new Exception("There are more than one entries with the same userName.");
            }
            SearchResultAttributeCollection attributes = searchResponse.Entries[0].Attributes;

            userFields.LogonTime         = GetAttributeFileTime(attributes, "lastLogon");
            userFields.LogonHours        = GetAttributeFileTime(attributes, "logonHours");
            userFields.AccountExpires    = GetAttributeFileTime(attributes, "accountExpires");
            userFields.LogoffTime        = GetLogoffTime(userFields.LogonHours, userFields.AccountExpires);
            userFields.KickOffTime       = GetKickoffTime(userFields.LogoffTime);
            userFields.PasswordLastSet   = GetAttributeFileTime(attributes, "pwdLastSet");
            userFields.dBCSPwd           = GetAttributeFileTime(attributes, "dBCSPwd");
            userFields.unicodePwd        = GetAttributeFileTime(attributes, "unicodePwd");
            userFields.PasswordCanChange = GetPasswordCanChange(userFields.dBCSPwd, userFields.unicodePwd, userFields.PasswordLastSet);

            object attributeValue = null;

            attributeValue = getAttributeValue(attributes, "userAccountControl");
            userFields.userAccountControl = (uint?)Convert.ToInt32(attributeValue);

            userFields.PasswordMustChange = GetPasswordMustChange(userFields.userAccountControl, userFields.PasswordLastSet);
            userFields.LogonCount         = GetAttributeUshort(attributes, "logonCount");
            userFields.BadPwdCount        = GetAttributeUshort(attributes, "badPwdCount");
            userFields.objectSid          = GetobjectSid(attributes);
            if (userFields.objectSid == null)
            {
                userFields.userId = null;
            }
            else
            {
                string[] tmp = userFields.objectSid.Split('-');
                userFields.userId = Convert.ToUInt32(tmp[tmp.Length - 1]);
            }

            attributeValue            = getAttributeValue(attributes, "primaryGroupID");
            userFields.primaryGroupId = (uint?)Convert.ToInt32(attributeValue);

            userFields.groupCount = GetGroupCount(attributes);
            if (userFields.groupCount > 0)
            {
                userFields.groupIds = GetGroupIds(attributes, domainName, cred);
            }

            userFields.domainSid = GetDomainSid(domainName, cred);
            return(userFields);
        }
        public static commonUserFields GetCommonUserFields(string domainName, string userName, NetworkCredential cred)
        {
            LdapConnection connection = new LdapConnection(domainName);
            connection.Credential = cred;
            string dn = GetDomainDnFromDomainName(domainName);
            string targetOu = "cn=Users," + dn;
            string filter = "cn=" + userName;
            string[] attributesToReturn = new string[] { "lastLogon", "logonHours", "accountExpires", "pwdLastSet", "dBCSPwd", "unicodePwd", "userAccountControl", "logonCount", "badPwdCount", "objectSid", "primaryGroupID", "memberOf" };
            commonUserFields userFields = new commonUserFields();

            SearchRequest searchRequest = new SearchRequest(targetOu, filter, SearchScope.Subtree, attributesToReturn);

            SearchResponse searchResponse = (SearchResponse)connection.SendRequest(searchRequest);
            if (searchResponse.Entries.Count > 1)
            {
                throw new Exception("There are more than one entries with the same userName.");
            }
            SearchResultAttributeCollection attributes = searchResponse.Entries[0].Attributes;

            userFields.LogonTime = GetAttributeFileTime(attributes, "lastLogon");
            userFields.LogonHours = GetAttributeFileTime(attributes, "logonHours");
            userFields.AccountExpires = GetAttributeFileTime(attributes, "accountExpires");
            userFields.LogoffTime = GetLogoffTime(userFields.LogonHours, userFields.AccountExpires);
            userFields.KickOffTime = GetKickoffTime(userFields.LogoffTime);
            userFields.PasswordLastSet = GetAttributeFileTime(attributes, "pwdLastSet");
            userFields.dBCSPwd = GetAttributeFileTime(attributes, "dBCSPwd");
            userFields.unicodePwd = GetAttributeFileTime(attributes, "unicodePwd");
            userFields.PasswordCanChange = GetPasswordCanChange(userFields.dBCSPwd, userFields.unicodePwd, userFields.PasswordLastSet);

            object attributeValue = null;
            attributeValue =  getAttributeValue(attributes, "userAccountControl");
            userFields.userAccountControl = (uint?) Convert.ToInt32(attributeValue);

            userFields.PasswordMustChange = GetPasswordMustChange(userFields.userAccountControl, userFields.PasswordLastSet);
            userFields.LogonCount = GetAttributeUshort(attributes, "logonCount");
            userFields.BadPwdCount = GetAttributeUshort(attributes, "badPwdCount");
            userFields.objectSid = GetobjectSid(attributes);
            if (userFields.objectSid == null)
            {
                userFields.userId = null;
            }
            else
            {
                string[] tmp = userFields.objectSid.Split('-');
                userFields.userId = Convert.ToUInt32(tmp[tmp.Length - 1]);
            }

            attributeValue = getAttributeValue(attributes, "primaryGroupID");
            userFields.primaryGroupId = (uint?)Convert.ToInt32(attributeValue);

            userFields.groupCount = GetGroupCount(attributes);
            if (userFields.groupCount > 0)
            {
                userFields.groupIds = GetGroupIds(attributes, domainName, cred);
            }

            userFields.domainSid = GetDomainSid(domainName, cred);
            return userFields;
        }