Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 需要澄清PrincipalContext安全权限和PrincipalContext';s ContextType.Machine_Asp.net Mvc_C# 4.0_Active Directory_Directoryservices_Principalcontext - Fatal编程技术网

Asp.net mvc 需要澄清PrincipalContext安全权限和PrincipalContext';s ContextType.Machine

Asp.net mvc 需要澄清PrincipalContext安全权限和PrincipalContext';s ContextType.Machine,asp.net-mvc,c#-4.0,active-directory,directoryservices,principalcontext,Asp.net Mvc,C# 4.0,Active Directory,Directoryservices,Principalcontext,如果我使用上面的代码查询Active Directory,并且PrincipalContext构造函数中传递的用户名(帐户)位于与目标域(要查询的域)不信任的域中,则会出现以下错误 System.DirectoryServices.AccountManagement.PrincipalServerDownException:无法联系服务器。-->System.DirectoryServices.Protocols.LdapException:LDAP服务器不可用 如果PrincipalConte

如果我使用上面的代码查询Active Directory,并且PrincipalContext构造函数中传递的用户名(帐户)位于与目标域(要查询的域)不信任的域中,则会出现以下错误

System.DirectoryServices.AccountManagement.PrincipalServerDownException:无法联系服务器。-->System.DirectoryServices.Protocols.LdapException:LDAP服务器不可用

如果PrincipalContext结构更改为

using (PrincipalContext Context = new PrincipalContext(ContextType.Domain, DomainURL, UserName, Password))
{
   UserPrincipal Account = new UserPrincipal(Context);
   Account.GivenName = strFirstName;
   Account.Surname = strLastName;

   PrincipalSearcher srch = new PrincipalSearcher(Account);
   foreach (var principal in srch.FindAll())
     {
        var p = (UserPrincipal)principal;
        String FirstName = p.GivenName;
        String LastName = p.Surname;
     }            
}
只要客户端在目标域中,代码就会成功执行

假设域a中的客户端尝试在域B中搜索用户信息时调用了第一个带有用户名和密码的代码,此处建立上下文失败,因为使用的帐户位于域a中,该域a与域B不信任


如果我将ContextType更改为Machine,并且调用代码的客户端位于域B中,那么代码将成功执行,这一假设是否正确?

否,这不是正确的假设
ContextType.Machine
表示您希望使用本地帐户

您的
PrincipalSearcher
最终将搜索本地SAM数据库,而不是Active Directory

 using (PrincipalContext ctx = new PrincipalContext(ContextType.Machine))