使用c#对活动目录有疑问

本文关键字:有疑问 活动 使用 | 更新日期: 2023-09-27 18:13:48

我有一个使用活动目录的web应用程序。我在参数(用户、密码和自己的域)中创建了一个函数来执行身份验证域。在determinat行显示一条消息"Unspecified error",如下所示:

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.
           ---> This Line occurred error
             **object obj = entry.NativeObject;** 
           ---> Line Above occurred error
            DirectorySearcher search = new DirectorySearcher(entry);
            search.Filter = "(SAMAccountName=" + username + ")";
            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);
        }
        return true;
    }

使用c#对活动目录有疑问

using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN"))
{
    // validate the credentials
    bool isValid = pc.ValidateCredentials("myuser", "mypassword")
}

这可能会更好地满足您的需求。您至少需要. net 3.5,并且应该使用. net 3.5进行身份验证。