Click here to Skip to main content
15,891,767 members
Home / Discussions / C#
   

C#

 
GeneralRe: i want to make an ip messenger can i get the information related on my project Pin
fly90421-Mar-09 12:38
fly90421-Mar-09 12:38 
GeneralRe: i want to make an ip messenger can i get the information related on my project Pin
shomic.goyal21-Mar-09 12:44
shomic.goyal21-Mar-09 12:44 
GeneralRe: i want to make an ip messenger can i get the information related on my project Pin
fly90421-Mar-09 12:48
fly90421-Mar-09 12:48 
GeneralRe: i want to make an ip messenger can i get the information related on my project Pin
Yusuf21-Mar-09 13:06
Yusuf21-Mar-09 13:06 
GeneralRe: i want to make an ip messenger can i get the information related on my project Pin
fly90421-Mar-09 13:10
fly90421-Mar-09 13:10 
JokeRe: i want to make an ip messenger can i get the information related on my project Pin
Yusuf21-Mar-09 13:18
Yusuf21-Mar-09 13:18 
GeneralRe: i want to make an ip messenger can i get the information related on my project Pin
Yusuf21-Mar-09 13:07
Yusuf21-Mar-09 13:07 
QuestionHelp cleaning/making LDAP access faster (remotely)? Pin
Jacob Dixon21-Mar-09 11:46
Jacob Dixon21-Mar-09 11:46 
Hello, I have been working on a application to help us better manage our network. The application is for myself and one other person at our agency. Everything is working fine, but doing this one thing (getting list of users) from LDAP remotely is very slow. Now I know it will be slower than normal, but how I have coded it might freak some of you out, and see how I could make what I am trying to do quicker.

I have used alot of the LDAP/AD coding from an article on this site (codeproject).
Thanks to:
http://www.codeproject.com/KB/system/everythingInAD.aspx[^]

First,
I have a ListView control that populates with every user. Every user has a icon (man icon) that either has a lock symbol (if the user is locked), a X symbol on it (if the user is disabled), or nothing if the user does not have either one of those.

1. Here is what it is doing when Adding to the ListView Control:
private delegate void AddListItemDelegate();
void AddListItem()
{
    if (listViewUsers.InvokeRequired)
    {
        listViewUsers.Invoke(new AddListItemDelegate(AddListItem));
    }
    else
    {
        if (!UseAuth) adPrincipalContext = new PrincipalContext(ContextType.Domain, Properties.Settings.Default.LDAP);
        else adPrincipalContext = new PrincipalContext(ContextType.Domain, Properties.Settings.Default.LDAP,
            (string)Encoding.Unicode.GetString(Convert.FromBase64String((string)Properties.Settings.Default.LDAPusername)),
            (string)Encoding.Unicode.GetString(Convert.FromBase64String((string)Properties.Settings.Default.LDAPpassword)));

        UserPrincipal user = new UserPrincipal(adPrincipalContext);
        user.Name = "*";
        PrincipalSearcher pS = new PrincipalSearcher();
        pS.QueryFilter = user;
        PrincipalSearchResult<Principal> results = pS.FindAll();

        listViewUsers.Items.Clear();

        ListViewItem listItem;
        foreach (Principal result in results)
        {
            AD newAD = new AD(result.Name);
            bool Locked = newAD.IsLocked();
            bool Enabled = newAD.IsEnabled();
            string Branch = newAD.branch;

            listItem = new ListViewItem(result.Name);
            listItem.ToolTipText = "Sid: " + result.Sid;

            if (Enabled)
            {
                if (Locked) listItem.ImageIndex = 1;
                else listItem.ImageIndex = 0;
            }
            else listItem.ImageIndex = 2;

            bool found = false;
            if (String.IsNullOrEmpty(Branch)) Branch = "Unknown";
            foreach (ListViewGroup g in listViewUsers.Groups)
            {
                if (g.Header == Branch) {
                    found = true;
                    listItem.Group = g;
                    break;
                }
            }
            if (!found)
            {
                ListViewGroup g = new ListViewGroup(Branch, Branch);
                listViewUsers.Groups.Add(g);
                listItem.Group = g;
            }
            listViewUsers.Items.Add(listItem);
        }
    }
}


So what I have this doing is getting a list of users from AD, but also going to my AD class to see if the user is LOCKED or DISABLED. This is what is happening behind the AD class:
try
            {
                using (DirectoryEntry root = new DirectoryEntry())
                {
                    root.Path = LDAP;

                    if (UseAuth)
                    {
                        root.Username = (string)Encoding.Unicode.GetString(Convert.FromBase64String((string)Properties.Settings.Default.LDAPusername));
                        root.Password = (string)Encoding.Unicode.GetString(Convert.FromBase64String((string)Properties.Settings.Default.LDAPpassword));
                    }


                    using (DirectorySearcher searcher = new DirectorySearcher())
                    {
                        searcher.SearchRoot = root;
                        searcher.SearchScope = SearchScope.Subtree;
                        searcher.Filter = "(&(objectClass=user)(name=" + this.userName + "))";
                        searcher.PropertiesToLoad.Add("mail");
                        searcher.PropertiesToLoad.Add("department");
                        searcher.PropertiesToLoad.Add("company");
                        searcher.PropertiesToLoad.Add("physicalDeliveryOfficeName");
                        searcher.PropertiesToLoad.Add("telephoneNumber");
                        SearchResultCollection results = searcher.FindAll();

                        if (results != null)
                        {
                            foreach (SearchResult result in results)
                            {
                                ResultPropertyCollection props = result.Properties;
                                foreach (string propName in result.Properties.PropertyNames)
                                {
                                    if (propName == "mail") mail = props[propName][0].ToString();
                                    else if (propName == "department") branch = props[propName][0].ToString();
                                    else if (propName == "company") division = props[propName][0].ToString();
                                    else if (propName == "physicalDeliveryOfficeName") office = props[propName][0].ToString();
                                    else if (propName == "telephoneNumber") officenumber = props[propName][0].ToString();
                                    else if (propName == "adspath") adspath = props[propName][0].ToString();
                                }
                            }
                        }

                        searcher.Dispose();
                        root.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


So what I am doing here is using the username provided from the previous class and passing it to here to set the username and password. If you look in the previous code you will see this:
AD newAD = new AD(result.Name);
bool Locked = newAD.IsLocked();
bool Enabled = newAD.IsEnabled();


The code I have for checking if it is locked:
public bool IsLocked()
{
    try
    {
        DirectoryEntry uEntry;
        if (UseAuth) uEntry = new DirectoryEntry(this.adspath, (string)Encoding.Unicode.GetString(Convert.FromBase64String((string)Properties.Settings.Default.LDAPusername)), (string)Encoding.Unicode.GetString(Convert.FromBase64String((string)Properties.Settings.Default.LDAPpassword)));
        else uEntry = new DirectoryEntry(this.adspath);

        bool Locked = Convert.ToBoolean(uEntry.InvokeGet("IsAccountLocked"));
        uEntry.Close();

        if (Locked) return true;
        else return false;
    }
    catch (DirectoryServicesCOMException ex)
    {
        return false;
    }
}

and
public bool IsEnabled()
{
    DirectoryEntry uEntry;
    if (UseAuth) uEntry = new DirectoryEntry(this.adspath, (string)Encoding.Unicode.GetString(Convert.FromBase64String((string)Properties.Settings.Default.LDAPusername)), (string)Encoding.Unicode.GetString(Convert.FromBase64String((string)Properties.Settings.Default.LDAPpassword)));
    else uEntry = new DirectoryEntry(this.adspath);

    int val = (int)uEntry.Properties["userAccountControl"].Value;

    uEntry.Close();

    if (val == (val & ~0x2)) return true;
    else return false;
}




So basically what I am doing is querying LDAP, getting a list of users, then going through each user and querying LDAP AGAIN for all of that USERS information, THEN with that information, querying LDAP again to see if the accounts unlocked, THEN querying LDAP again to see if the account is DISABLED.

So my question is... how exactly would I clean this up? I am new to using C# with AD and trying to make it work. Well it does work fine when on the internal network, its slow outside. I know it can be coded better and I'm not asking anyone to do it, just point me (or shove me) in the right direction to what I should change LOL.

Thanks in advanced!
AnswerRe: Help cleaning/making LDAP access faster (remotely)? Pin
Yusuf21-Mar-09 13:10
Yusuf21-Mar-09 13:10 
GeneralRe: Help cleaning/making LDAP access faster (remotely)? Pin
Jacob Dixon22-Mar-09 4:47
Jacob Dixon22-Mar-09 4:47 
GeneralRe: Help cleaning/making LDAP access faster (remotely)? Pin
Jacob Dixon22-Mar-09 4:55
Jacob Dixon22-Mar-09 4:55 
GeneralRe: Help cleaning/making LDAP access faster (remotely)? Pin
Jacob Dixon22-Mar-09 4:58
Jacob Dixon22-Mar-09 4:58 
GeneralRe: Help cleaning/making LDAP access faster (remotely)? Pin
Jacob Dixon22-Mar-09 5:42
Jacob Dixon22-Mar-09 5:42 
QuestionLotus Notes......Configure probelm [modified] Pin
lokesh143.surana21-Mar-09 10:37
lokesh143.surana21-Mar-09 10:37 
AnswerRe: Lotus Notes......Configure probelm Pin
Henry Minute21-Mar-09 10:41
Henry Minute21-Mar-09 10:41 
QuestionCalculate size of encrypted data Pin
forum_user21-Mar-09 9:59
forum_user21-Mar-09 9:59 
AnswerRe: Calculate size of encrypted data Pin
Henry Minute21-Mar-09 10:52
Henry Minute21-Mar-09 10:52 
QuestionConvert String to set it on richBox Pin
abbd21-Mar-09 9:42
abbd21-Mar-09 9:42 
AnswerRe: Convert String to set it on richBox Pin
DaveyM6921-Mar-09 10:06
professionalDaveyM6921-Mar-09 10:06 
GeneralRe: Convert String to set it on richBox Pin
abbd21-Mar-09 10:08
abbd21-Mar-09 10:08 
GeneralRe: Convert String to set it on richBox Pin
DaveyM6921-Mar-09 10:16
professionalDaveyM6921-Mar-09 10:16 
AnswerRe: Convert String to set it on richBox Pin
c0ax_lx21-Mar-09 11:48
c0ax_lx21-Mar-09 11:48 
GeneralRe: Convert String to set it on richBox Pin
fly90421-Mar-09 12:44
fly90421-Mar-09 12:44 
QuestionCryptography Pin
kenexcelon21-Mar-09 9:37
kenexcelon21-Mar-09 9:37 
AnswerRe: Cryptography Pin
Ravadre21-Mar-09 16:33
Ravadre21-Mar-09 16:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.