如何提供DirectoryEntry.Exists凭据? [英] How to provide DirectoryEntry.Exists with credentials?

查看:334
本文介绍了如何提供DirectoryEntry.Exists凭据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早上,我发现了一个不错的方法(的DirectoryEntry。存在),这应该是能够检查的Active Directory对象是否存在于服务器上。于是,我就用一个简单的:

This morning I discovered a nice method (DirectoryEntry.Exists), that should be able to check whether an Active Directory object exists on the server. So I tried with a simple:

if (DirectoryEntry.Exists(path)) {}

当然,它没有任何的过载提供凭据它。因为,如果不提供凭据,我得到这个异​​常:

Of course it lacks any overloads to provide credentials with it. Because, if credentials are not provided I get this Exception:

登录失败:未知的用户名或   密码错误。   (System.DirectoryServices.DirectoryServicesCOMException)

Logon failure: unknown user name or bad password. (System.DirectoryServices.DirectoryServicesCOMException)

是否有任何其他选项,让我在AD服务器来验证我的code的可能性?或者检查对象的存在?

Is there any other option that gives me the possibility to authenticate my code at the AD server? Or to check the existence of an object?

推荐答案

在这种情况下,你不能用静态的方法,你说的存在:

In this case you can't use the static method Exists as you said :

DirectoryEntry directoryEntry = new DirectoryEntry(path);
directoryEntry.Username = "username";
directoryEntry.Password = "password";

bool exists = false;
// Validate with Guid
try
{
    var tmp = directoryEntry.Guid;
    exists = true;
}
catch (COMException)
{
   exists = false; 
}

这篇关于如何提供DirectoryEntry.Exists凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆