【发布时间】:2009-12-29 19:11:42
【问题描述】:
我在从 Web 服务调用的类中有以下代码:
NetworkCredential credentials = new NetworkCredential("user", "password");
connection = new LdapConnection("domain");
connection.Bind(credentials);
DirectoryContext directoryContext =
new DirectoryContext(DirectoryContextType.Domain, "domain");
// This call returns a domain object with unreadable properties
Domain domain = Domain.GetDomain(directoryContext);
如果我直接实例化类,一切都很好,我有一个可以使用的有效域对象。如果我通过 Web 服务,将创建域对象,但大多数属性都会抛出异常,例如:
'domain.Children' threw an exception of type ActiveDirectoryOperationException
我已启用模拟,并在调用 Web 服务之前明确设置凭据。在 Web 服务端检查 Thread.CurrentPrincipal.Identity.Name 会显示我明确设置的凭据的用户名。
如果我查看Request.LogonUserIdentity,我有以下属性:
Name: "domain\\username" (is correct)
ImpersonationLevel: Impersonation
IsAnonymous: false
IsAuthenticated: true
AuthenticationType: NTLM
匿名访问被禁用(启用它没有区别),并且“基本身份验证”和“集成 Windows 身份验证”都被选中。 Web 服务在我的开发盒上的 IIS 5.1 下运行。
调用 Web 服务的代码,导致对 Domain.GetDomain() 的调用失败:
MyServiceProxy proxy = new MyServiceProxy ();
CredentialCache credCache = new CredentialCache();
NetworkCredential netCred = new NetworkCredential(user, password, domain);
credCache.Add(new Uri(proxy.Url), "Ntlm", netCred);
proxy.Credentials = credCache;
proxy.MethodCall();
直接调用成功的代码:
MyService myService = new MyService();
myService.MethodCall();
您知道为什么在 Web 服务的上下文中调用 Active Directory 会失败吗?再一次,调用本身并没有失败......它返回一个具有不可读属性的域对象。
提前致谢!
【问题讨论】:
-
我很欣赏 Per Noalt 的意见,他提供了一些有价值的信息,但正如我在他回答下的 cmets 中解释的那样,我无法访问他在回答中使用的对象,因为我不是从网页调用。因此它没有回答这个问题。 Per 的帮助值得称赞,我可以将他的答案标记为已接受,但这不会误导其他来这里寻找相同问题答案的人吗?
标签: c# active-directory dns