LDAP获取组名称

时间:2013-04-29 15:09:54

标签: asp.net ldap c#-2.0

当我尝试获取用户所属的组时,我收到“登录失败:未知用户名或密码错误”错误。用户身份验证工作正常,这是我无法理解的。如何根据AD正确验证用户身份但无法获取其组名? 我得到用户的ID和密码。我有一个处理身份验证的课程。

        if ((true == adAuth.IsAuthenticated(sDomain, sID, sPassword)))
        {
            string sGroups = adAuth.GetGroups();

这是身份验证类:

public class LdapAuthentication
{
    string _path;
    string _filterAttribute;

     public LdapAuthentication(string path)
    {
        _path = path;
    }

public bool IsAuthenticated(string domain, string username, string pwd)
{
    string domainAndUsername = domain + "\\" + username;
    DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);

    try {
        //Bind to the native AdsObject to force authentication.         
        object obj = entry.NativeObject;
        DirectorySearcher search = new DirectorySearcher(entry);

        search.Filter = "(SAMAccountName=" + username + ")";
        search.PropertiesToLoad.Add("cn");
        SearchResult result = search.FindOne();

        if ((result == null)) {
            return false;
        }

        //Update the new path to the user in the directory.
        _path = result.Path;
        _filterAttribute = Convert.ToString(result.Properties["cn"][0]);

        } 
        catch (Exception ex) {
        throw new Exception("Error authenticating user. " + ex.Message);
            //return false;
        }

        return true;
    }

public string GetGroups()
{
    //DirectorySearcher search = new DirectorySearcher(_path);

        // Use following two lines instead of the above to handle cases of authenticatin against an LDAP server other than local AD domain
        DirectoryEntry deSearchRoot = new DirectoryEntry(_path);
        DirectorySearcher search = new DirectorySearcher(deSearchRoot);

            search.Filter = "(cn=" + _filterAttribute + ")";
        search.PropertiesToLoad.Add("memberOf");
        StringBuilder groupNames = new StringBuilder();

        try {
            SearchResult result = search.FindOne();
            int propertyCount = result.Properties["memberOf"].Count;

            string dn = null;
            int equalsIndex = 0;
            int commaIndex = 0;

            int propertyCounter = 0;

            for (propertyCounter = 0; propertyCounter <= propertyCount - 1; propertyCounter++) {
                dn = Convert.ToString(result.Properties["memberOf"][propertyCounter]);

                equalsIndex = dn.IndexOf("=", 1);
                commaIndex = dn.IndexOf(",", 1);
                if ((equalsIndex == -1)) {
                    return null;
                }

                groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
                groupNames.Append("|");
            }

        } catch (Exception ex) {
            throw new Exception("Error obtaining group names. " + ex.Message);
        }

        return groupNames.ToString();
    }

IsAuthnticated通过并正常工作; GetGroups()返回“获取组名称时出错”,然后是“登录失败:未知用户名或密码错误”(即GetGroups()中的异常)。

当我从VS运行应用程序时它工作正常但是当我发布它(在同一台服务器上)时,它的行为就像这样。 任何想法都非常感激。

1 个答案:

答案 0 :(得分:0)

没关系;操作员错误。代码工作正常。