以下代码适用于域管理员。但不具有用户权限。
using System;
using System.Collections.Generic;
using System.DirectoryServices.AccountManagement;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WinM
{
class Program
{
static void Main(string[] args)
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
}
}
}
如果我以用户身份运行它,我会收到以下错误:
Unhandled Exception: System.DirectoryServices.Protocols.LdapException: The connection cannot be established.
at System.DirectoryServices.Protocols.ConnectionHandle..ctor(IntPtr value, Boolean disposeHandle)
at System.DirectoryServices.Protocols.LdapConnection.Init()
at System.DirectoryServices.Protocols.LdapConnection..ctor(LdapDirectoryIdentifier identifier, NetworkCredential credential, AuthType authType)
at System.DirectoryServices.Protocols.LdapConnection..ctor(LdapDirectoryIdentifier identifier)
at System.DirectoryServices.Protocols.LdapConnection..ctor(String server)
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)
at WinM.Program.Main(String[] args) in
有人有想法吗?