【www.gdgbn.com--安卓教程】

1:如何获取域中某个组的所有用户



private void Page_Load(object sender, System.EventArgs e)
...{
    StringCollection groupMembers = this.GetGroupMembers("pardesifashions","Debugger Users");
    foreach (string strMember in groupMembers)
    ...{
        Response.Write("
" + strMember + "");
    }
}

public StringCollection GetGroupMembers(string strDomain, string strGroup)
...{
    StringCollection groupMemebers = new StringCollection();
    try
    ...{
        DirectoryEntry ent = new DirectoryEntry("LDAP://DC=" + strDomain + ",DC=com");
        DirectorySearcher srch = new DirectorySearcher("(CN=" + strGroup + ")");
        SearchResultCollection coll = srch.FindAll();
        foreach (SearchResult rs in coll)
        ...{
            ResultPropertyCollection resultPropColl = rs.Properties;
            foreach( Object memberColl in resultPropColl["member"])
            ...{
                DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://" + memberColl);
                System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties;
                object obVal = userProps["sAMAccountName"].Value;
                if (null != obVal)
                ...{
                    groupMemebers.Add(obVal.ToString());
                }
            }
        }
    }
    catch (Exception ex)
    ...{
        Trace.Write(ex.Message);
    }
    return groupMemebers;
}         
2:获取某个AD中所有的域


private void Page_Load(object sender, System.EventArgs e)
...{
    StringCollection adDomains = this.GetDomainList();
    foreach (string strDomain in adDomains)
    ...{
        Response.Write("
" + strDomain + "");
    }
}

private StringCollection GetDomainList()
...{
    StringCollection domainList = new StringCollection();
    try
    ...{
        DirectoryEntry en = new DirectoryEntry("LDAP://");
        // Search for objectCategory type "Domain"
        DirectorySearcher srch = new DirectorySearcher("objectCategory=Domain");
        SearchResultCollection coll = srch.FindAll();
        // Enumerate over each returned domain.
        foreach (SearchResult rs in coll)
        ...{
            ResultPropertyCollection resultPropColl = rs.Properties;
            foreach( object domainName in resultPropColl["name"])
            ...{
                domainList.Add(domainName.ToString());
            }
        }
    }
    catch (Exception ex)
    ...{
        Trace.Write(ex.Message);
    }
    return domainList;
}            
3:获取当前登录用户的全名


private void Page_Load(object sender, System.EventArgs e)
...{
    WindowsPrincipal p = Thread.CurrentPrincipal as WindowsPrincipal;
    Response.Write(GetFullName(p.Identity.Name));
}

private string GetFullName(string strLogin)
...{
    string str = "";
    // Parse the string to check if domain name is present.
    int idx = strLogin.IndexOf("""");
    if (idx == -1)
    ...{
        idx = strLogin.IndexOf(""@"");
    }

    string strDomain;
    string strName;

    if (idx != -1)
    ...{
        strDomain = strLogin.Substring(0, idx);
        strName = strLogin.Substring(idx+1);
    }
    else
    ...{
        strDomain = Environment.MachineName;
        strName = strLogin;
    }

    DirectoryEntry obDirEntry = null;
    try
    ...{
        obDirEntry = new DirectoryEntry("WinNT://" + strDomain + "/" + strName);
        System.DirectoryServices.PropertyCollection  coll = obDirEntry.Properties;
        object obVal = coll["FullName"].Value;
        str = obVal.ToString();
    }
    catch (Exception ex)
    ...{
        str = "";
        Trace.Write(ex.Message);
    }
    return str;
}        

本文来源:http://www.gdgbn.com/shoujikaifa/12993/