LDAP - 从IIS获取用户的组名失败

时间:2012-12-11 13:40:11

标签: c# asp.net iis ldap

我按照tutorial在ASP.NET / C#4.0 Web应用程序上实现LDAP身份验证。虽然事情确实有效,但当我把网站放在IIS7下时,却无法获得组名。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Collections;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;

namespace KEWeb
{
    public class LdapAuthentication
    {
        private string _path;
        private string _filterAttribute;
        private string _username;
        private string _password;
        private string _domain;
        private string _domainAndUsername;
        private DirectoryEntry _entry;
        private DirectorySearcher _search;
        private SearchResult _result;

        public LdapAuthentication(string path, string domain, string username, string password)
        {
            _path = path;
            _domain = domain;
            _username = username;
            _password = password;
            _domainAndUsername = _domain + @"\" + _username;
            _entry = new DirectoryEntry(_path, _domainAndUsername, _password);

        }

        public bool IsAuthenticated()
        {
            try
            {
                Object obj = _entry.NativeObject;
                _search = new DirectorySearcher(_entry);
                _search.Filter = "(SAMAccountName=" + _username + ")";
                _search.PropertiesToLoad.Add("cn");
                _result = _search.FindOne();
                if (null == _result) { return false; }
                _path = _result.Path;
                _filterAttribute = (String)_result.Properties["cn"][0];
            }
            catch (Exception ex) { throw new Exception("Error authenticating user: " + ex.Message); }
            return true;
        }

        public string GetGroups()
        {
            string r = "";
            try
            {
                Object obj = _entry.NativeObject;
                _search = new DirectorySearcher(_entry);
                _search.Filter = "(SAMAccountName=" + _username + ")";
                _search.PropertiesToLoad.Add("cn");
                _result = _search.FindOne();
                if (null != _result)
                {
                    _path = _result.Path;
                    _filterAttribute = (String)_result.Properties["cn"][0];
                    _search = new DirectorySearcher(_path);
                    _search.Filter = "(cn=" + _filterAttribute + ")";
                    _search.PropertiesToLoad.Add("memberOf");
                    StringBuilder groupNames = new StringBuilder();
                    _result = _search.FindOne();
                    int propertyCount = _result.Properties["memberOf"].Count;
                    String dn;
                    int equalsIndex, commaIndex;
                    for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
                    {
                        dn = (String)_result.Properties["memberOf"][propertyCounter];
                        equalsIndex = dn.IndexOf("=", 1);
                        commaIndex = dn.IndexOf(",", 1);
                        if (-1 == equalsIndex) { return null; }
                        groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
                        groupNames.Append("|");
                    }
                    r = groupNames.ToString();
                }
            }
            catch (Exception ex) { throw new Exception("Error obtaining group names: " + ex.Message); }

            return r;
        }
    }
}

在Visual Studio 2010的调试中运行时,它可以正常工作。但是在IIS7上,它会出现错误An operations error occured.不确定如何在IIS下调试它,尽管我确信这是可能的。如果我完全忽略GetGroups()并将该代码注释掉,那就可以了,但我当然需要这些组名。

PS - 是的,上面的代码与原版完全不同,我将其调整为重用一些冗余的东西。然而,在我改变之前,我确实遇到了这个问题。

3 个答案:

答案 0 :(得分:2)

您正在使用错误的项目搜索群组 - 您需要使用:

_search = new DirectorySearcher(_path);
GetGroups()来电中

_path变量由IsAuthenticated()调用设置。

答案 1 :(得分:1)

如果附加到w3wp进程,则可以在iis下调试应用程序。使用CTRL + Alt + P热键连接到它,或者转到Debug菜单并选择Attach to Process ...并选中“在所有会话中显示进程”复选框。

答案 2 :(得分:0)

这可能为时已晚,无法提供帮助,但我遇到了完全相同的问题。

对我而言,IIS网站已启用ASP.Net模拟和匿名,并且模拟用户已配置为使用经过身份验证的用户。由于Anonymous也已启用,因此匿名用户无权访问AD,因此总是会失败。

更改了ASP.Net模拟以使用有效的AD帐户,瞧!

约翰

PS。我也和你一样在同一个教程中工作。 :)