Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.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
C# 如何登录到Active Directory?_C#_.net 4.0_Active Directory_Authorization_Windows Server - Fatal编程技术网

C# 如何登录到Active Directory?

C# 如何登录到Active Directory?,c#,.net-4.0,active-directory,authorization,windows-server,C#,.net 4.0,Active Directory,Authorization,Windows Server,我有active directory的用户名和密码,我想使用C#登录active directory。如何在Windows窗体中执行此操作?解决方案 连接到Active Directory非常容易 必须使用DirectoryEntry对象(在命名空间System.DirectoryServices中) 此对象的构造函数在参数中包含三个字符串: Active Directory的路径。此路径的格式为:LDAP://您的姓名广告 连接的用户名 相应的密码 例子 好了。此方法根据Active Di

我有active directory的用户名和密码,我想使用C#登录active directory。如何在Windows窗体中执行此操作?

解决方案 连接到Active Directory非常容易

必须使用
DirectoryEntry
对象(在命名空间
System.DirectoryServices
中)

此对象的构造函数在参数中包含三个字符串:

  • Active Directory的路径。此路径的格式为:
    LDAP://您的姓名广告
  • 连接的用户名
  • 相应的密码
例子
好了。此方法根据Active Directory验证
用户名/密码
,并且在相当长一段时间内一直是我的函数工具箱的一部分

//NOTE: This can be made static with no modifications
public bool ActiveDirectoryAuthenticate(string username, string password)
{
    bool result = false;
    using (DirectoryEntry _entry = new DirectoryEntry())
    {
        _entry.Username = username;
        _entry.Password = password;
        DirectorySearcher _searcher = new DirectorySearcher(_entry);
        _searcher.Filter = "(objectclass=user)";
        try
        {
            SearchResult _sr = _searcher.FindOne();
            string _name = _sr.Properties["displayname"][0].ToString();
            result = true;
        }
        catch
        { /* Error handling omitted to keep code short: remember to handle exceptions !*/ }
    }

    return result; //true = user authenticated!
}

显然,执行此操作的软件必须在域内的计算机上运行(否则您将没有Active Directory来验证您的凭据)。

这个问题是几年前提出的,我希望这个答案可以帮助未来的人们。 这对我有用:

添加以下参考:

  • 使用System.DirectoryServices
  • 使用System.DirectoryServices.AccountManagement
之后,您可以在应用程序中使用以下代码:

   PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOUR DOMAIN");
   bool Valid = pc.ValidateCredentials("User", "password");
如果登录正常,名为:Valid的变量将显示True


有关更多信息,请访问。该页面就是从这里开始的,stackOverFlow,它将向您显示有关“使用MS Active Directory登录”的大量信息一个简短的答案是添加参考System.DirectoryServices.AccountManagement

然后使用 UserPrincipal.Current.Context.ValidateCredentials(“用户名”、“密码”)


但是我想你需要加入到你想要验证的域中。

你到底想要实现什么?登录-然后呢?调用一些具有
WS-Http绑定的
WCF
服务
,那么您想登录到Active Directory还是要登录到域中的Windows Server?我想登录到Active Directory您不断重复-详细说明!你想做什么?您想完成什么?什么是
DirectoryEntry
?我想使用System.Security.Principal创建用户
感谢您的解决方案与
网络凭据cred=新网络凭据(“用户”、“通过”、“域”)之间的区别?你能解释更多吗?请处理在广告上执行的所有操作;不是。您的解决方案和NetworkCredential cred=新的NetworkCredential(“用户”、“通过”、“域”)之间有什么区别;?你能解释一下吗?WorkCredential只是一个包含一些数据的对象,它不验证任何东西。
   PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOUR DOMAIN");
   bool Valid = pc.ValidateCredentials("User", "password");