Vít*_*ins 5 c# asp.net directoryservices active-directory
我正在尝试这个代码:
public bool isTravelAdmin(string srvr, string usr, string password)
{
System.Diagnostics.Debug.WriteLine("I'm in isTravelAdmin!");
PrincipalContext domainctx = new PrincipalContext(ContextType.Domain, srvr);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(domainctx, IdentityType.SamAccountName, usr);
bool isMember = userPrincipal.IsMemberOf(domainctx, IdentityType.Name, "traveladmin");
if (isMember)
{
System.Diagnostics.Debug.WriteLine("This user is INDEED a member of that group");
return true;
}
else
{
System.Diagnostics.Debug.WriteLine("This user is *NOT* member of that group");
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
哪个应该检查用户是否属于某个组(“traveladmin”),但我得到
System.DirectoryServices.AccountManagement.PrincipalServerDownException
知道为什么以及如何解决吗?顺便一提:
srvr = "LDAP://192.168.56.101/CN=Users,DC=estagioit,DC=local"
Run Code Online (Sandbox Code Playgroud)
PS:我在另一种方法上使用相同的 srvr,它正在工作和连接。
PSS:如果这不是最好的方法,我愿意接受建议。
问题是“主要上下文”如何写......它应该是:
PrincipalContext thisPrincipalContext = new PrincipalContext(ContextType.Domain, "DCESTAGIO");
Run Code Online (Sandbox Code Playgroud)
在这种情况下。
如果你查看PrincipalContext构造函数的文档,它应该很清楚:
public PrincipalContext(ContextType contextType, string name)
Run Code Online (Sandbox Code Playgroud)
或者
public PrincipalContext(ContextType contextType, string name, string container)
Run Code Online (Sandbox Code Playgroud)
所以你基本上需要:
ContextType.Domain:)LDAP://前缀)正如这个答案中所见。
在你的情况下只需将你的更改srvr为:
srvr = "DCESTAGIO"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16539 次 |
| 最近记录: |