PrincipalContext无法连接

15
我正试图在我开发的 Web 服务中使用 PrincipalContext。我已经在另一个应用程序上使用表单身份验证,它可以正常工作。 我收到的错误信息是:
System.DirectoryServices.AccountManagement.PrincipalServerDownException: The server could not be contacted. ---> System.DirectoryServices.Protocols.LdapException: The LDAP server is unavailable.
   at System.DirectoryServices.Protocols.LdapConnection.Connect()
   at System.DirectoryServices.Protocols.LdapConnection.SendRequestHelper(DirectoryRequest request, Int32& messageID)
   at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request, TimeSpan requestTimeout)
   at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request)
   at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties& properties)
   --- End of inner exception stack trace ---
   at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties& properties)
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval()
   at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, ContextOptions options, String userName, String password)
   at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, String userName, String password)
   at webService.Service1.ValidUser(String sUserName) in E:\Development\CSharpApps\Desktop\OrgChart\webService\Service1.asmx.cs:line 158
我们的网络服务器在DMZ中,通过防火墙访问域。我将以下端口信息作为示例。
这样做使用我的开发框内的IP是有效的,但它在防火墙内。我发送给它的IP信息与我在Web表单认证中使用的相同。
 PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "192.168.1.1:389", "dc=doodlie,dc=com",@"doodlie\admin","doodliesquat");

一个类似的问题在这里:https://dev59.com/gHNA5IYBdhLWcg3wS7sm - mmcglynn
3个回答

1
也许我漏掉了什么,但实际上您并不需要指定AD服务器,您只需简单地说:
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

它应该在当前应用程序域中找到任何可找到的DC。如果是具有容错能力的网络,当一个不可用时,另一个应该接管。我不确定为什么会有理由特别访问其中一个,就像原始问题中的代码一样,除非它在不同的域中。如果是这种情况,您可以尝试在该域上托管您的Web服务,并使用DNS和转发器调用/路由到新域上的Web服务的新IP,如果需要,或者使用Hosts文件条目,或者只是通过IP引用Web服务。

0
无论问题是什么,安装一些不可或缺的AD管理/故障排除工具对我来说都是一个上天的恩赐。
如果可能,在您的计算机/或Web服务器(如果允许)上安装远程服务器管理工具(RSAT),然后使用Active Directory用户和计算机客户端确定您的DC的确切URL / ip。如果您无法使用这些工具连接,则可能是升级到IT支持/ dev ops的起点。
此外,网站应用程序正在运行的AD /服务帐户可能没有足够的权限访问DC。我已经成功了。
using (HostingEnvironment.Impersonate())
{
    // code in here. 
}

网站应用程序在IIS中运行的应用程序池应该在具有适当特权的用户帐户下运行。(不仅仅是网络服务)


0
在我的情况下,从URL中删除端口号起作用了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接