1

我正在使用一个 asp.net Web 应用程序。我使用表单身份验证来验证来自活动目录的用户。一切正常。但是,我很困惑为什么我应该将 AdsObject 绑定到一个从未使用过的本机对象。我的代码是这样的

 DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
        string[] uName;
        try
        {
            //Bind to the native AdsObject to force authentication.
            object obj = entry.NativeObject;

            DirectorySearcher search = new DirectorySearcher(entry);
            uName = domainAndUsername.Split('\\');
            search.Filter = "(SAMAccountName=" + uName[1] + ")";
            search.PropertiesToLoad.Add("cn");
            SearchResult result = search.FindOne();

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

            //Update the new path to the user in the directory.
            _path = result.Path;
            _filterAttribute = (string)result.Properties["cn"][0];
        }
        catch (Exception ex)
        {
            throw new Exception("Error authenticating user. " + ex.Message);
        }

我什至评论了这条线并对其进行了测试。它也工作得很好。我到处寻找答案,从未找到答案。希望我能在这里得到它.. :)

4

1 回答 1

1

虽然我在 MSDN 文档中的任何地方都找不到它,但检索 NativeObject 会强制进行身份验证。认证失败时会抛出异常。

于 2014-05-11T21:20:38.557 回答