C#代码无法更改AD密码。 (System.Runtime.InteropServices.COMException)

时间:2018-02-05 06:41:38

标签: c# asp.net active-directory ldap

我正在尝试创建一个简单的网站来更改我的AD密码。该网站将在我的IIS内部托管。

以下是我的用户界面的样子: Here

单击提交按钮后,我收到此错误(我使用asp.net标签显示): this image

System.Runtime.InteropServices.COMException (0x8007203A): The server is not operational.
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject()
at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
at System.DirectoryServices.DirectorySearcher.FindOne()
at changep1.changep.ChangeMyPassword(String domainName, String userName, String currentPassword, String newPassword) 

这是我的代码:

using System.DirectoryServices;

protected void btncp_Click(object sender, EventArgs e)
{
    string result = "";
    string username = txtuser.Text;
    string oldpass = txtoldpass.Text;
    string newpass2 = txtpassnew2.Text;
    string dn = "Sales-comm";

    result =ChangeMyPassword(dn, username, oldpass , newpass2);
    lblresult.Text = result;

}


public string ChangeMyPassword(string domainName, string userName, string currentPassword, string newPassword)
{
    string resultinner="";

    try
    {
        string ldapPath = "LDAP://commonspace.Sales-comm.local/CN=admin,CN=Accounts and Services,DC=Sales-comm,DC=local";
        DirectoryEntry directionEntry = new DirectoryEntry(ldapPath, domainName + "\\" + userName, currentPassword);
        if (directionEntry != null)

        {
            DirectorySearcher search = new DirectorySearcher(directionEntry);
            search.Filter = "(SAMAccountName=" + userName + ")";
            SearchResult result = search.FindOne();
            if (result != null)
            {
                DirectoryEntry userEntry = result.GetDirectoryEntry();
                if (userEntry != null)
                {
                    userEntry.Invoke("ChangePassword", new object[] { currentPassword, newPassword });
                    userEntry.CommitChanges();
                }
            }
        }
    }
    catch (Exception ex)
    {
        resultinner= ex.ToString();
    }

    return resultinner;
}

我检查了一些类似于我的Stack Overflow问题,并相应地更改了LDAP路径,但我仍然无法解决这个问题。有谁知道什么是错的?

1 个答案:

答案 0 :(得分:0)

这意味着它无法与域控制器联系。所以你必须做一些诊断。

执行DNS查找并确保您有结果:

nslookup commonspace.Sales-comm.local

尝试连接各种LDAP端口。我使用telnet客户端:

telnet commonspace.Sales-comm.local 389

这些是您可以使用的端口:

LDAP:389 全球目录:3268 LDAP over SSL:646 SSL全球目录:3269

如果您发现3268已打开,则可以使用GC://进行绑定:

string ldapPath = "GC://commonspace.Sales-comm.local/CN=admin,CN=Accounts and Services,DC=Sales-comm,DC=local";

如果其中一个SSL端口已打开,则您需要使用LDAP://但包含端口号:

string ldapPath = "LDAP://commonspace.Sales-comm.local:646/CN=admin,CN=Accounts and Services,DC=Sales-comm,DC=local";

如果您使用SSL,则可能必须确保在您连接的计算机上信任所使用的根证书。

相关问题