我有一个活动目录的用户名和密码,我想用C#登录到活动目录。我如何在Windows窗体中执行此操作?如何登录到Active Directory?
回答
你去那里。该方法针对Active Directory验证了username/password
,并且在相当长一段时间内一直是我的函数工具箱的一部分。
//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来验证您的凭据反对)。
解决方案
连接到Active Directory非常容易。
您必须使用DirectoryEntry
对象(在命名空间System.DirectoryServices
中)。
此对象的构造函数中的参数三个字符串:
- 路径到Active Directory。该路径的格式为:
LDAP://your-name-AD
- 的用户名进行连接
- 相应的密码
例
using System.DirectoryServices;
try
{
DirectoryEntry Ldap = new DirectoryEntry("LDAP://your-name-AD", "Login", "Password");
}
catch(Exception Ex)
{
Console.WriteLine(Ex.Message);
}
什么是“DirectoryEntry”?我想用户'使用System.Security.Principal;'谢谢 – Arian 2012-07-26 08:26:24
你的解决方案和'NetworkCredential cred = new NetworkCredential(“User”,“Pass”,“Domain”)有什么区别?你能解释更多请 – Arian 2012-07-26 08:37:52
['DirectoryEntry'](http://msdn.microsoft.com/en-us//library/system.directoryservices.directoryentry(v = vs.80).aspx)处理所有在广告 ;而不是['NetworkCredential'](http://msdn.microsoft.com/en-us//library/system.net.networkcredential(v = vs.80).aspx)。 – 2012-07-26 08:56:42
这个问题来自几年前,我希望这个答案可以帮助未来的人。 这是为我工作:
添加这些引用:
- 使用的System.DirectoryServices;
- using System.DirectoryServices.AccountManagement;
之后,你就可以在你的应用程序中使用此代码:
PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOUR DOMAIN");
bool Valid = pc.ValidateCredentials("User", "password");
称为变量:有效,会告诉你一个真实的值如果登录就可以了。
欲了解更多信息,你可以访问this page。该网页是从这里开始,计算器,它会告诉你很多有关信息:
更短的答案是“与MS Active Directory登录”添加参考System.DirectoryServices.AccountManagement
然后使用 UserPrincipal.Current.Context.ValidateCredentials(“username”,“password”);
但我想你需要加入到你想要验证的域中。
- 1. Active Directory登录
- 2. Azure Active Directory登录
- 3. ASP.NET Active Directory自动登录
- 4. 的Active Directory登录问题
- 5. Azure Active Directory单点登录
- 6. MSonline Active Directory自动登录
- 7. 使用C#登录到Active Directory
- 8. 如何使用Active Directory或LDAP登录到Business Objects
- 9. 更新Active Directory用户登录域名
- 10. 支持单点登录Active Directory
- 11. Ubuntu服务器和Active Directory组登录
- 12. 使用SSL验证LDAP/Active Directory登录
- 13. MVC ASP.Net本地Active Directory登录
- 14. Apache shiro Active Directory通过域名登录
- 15. Active Directory检查用户是否登录
- 16. IIS单点登录Active Directory帐号
- 17. Active Directory通过登录检查
- 18. Azure中的Active Directory登录错误
- 19. 使用Active Directory登录的IE WebDriver Selenium
- 20. Azure Active Directory单一登录超时
- 21. Flex - LDAP/Active Directory登录系统 - AMFPHP
- 22. 如何同时启用社交登录和Active Directory登录WordPress的
- 23. 如何监视Active Directory用户登录/注销?
- 24. 如何使用登录人员的凭据来搜索Active Directory?
- 25. 如何从Active Directory中检索用户的登录名?
- 26. 如何在Azure Active Directory中使用自定义角色登录
- 27. 如何在Active Directory中更改用户的登录名
- 28. 如何获取用户上次登录的Active Directory
- 29. 如何显示登录用户使用PHP的Active Directory全名?
- 30. 如何获取Azure Active Directory的密码策略登录用户
你想要达到什么样的目的?登录 - 然后呢? – abatishchev 2012-07-26 08:24:25
调用一些具有WS-Http绑定的'WCF'服务 – Arian 2012-07-26 08:25:36
那么你想要登录到Active Directory或进入域中的Windows Server吗? – abatishchev 2012-07-26 08:26:40