使用C#连接活动目录 [英] connect active directory using c#
问题描述
我尝试连接到Active Directory code,我是用
字符串域=domain.com.pk;
字符串容器=DC = mycompnay,DC = COM,DC = PK;
串管理员=salman.zafar;
字符串密码=密码;
使用(PrincipalContext PC =新PrincipalContext(ContextType.Domain,域,容器,admin,密码))
{
字符串的UserPrincipalName =dotnettest+@+域;
//验证凭据
BOOL的isValid = pc.ValidateCredentials(UserPrincipalName进行,Ascertia 12);
如果(参考isValid){
UserPrincipal起来= UserPrincipal.FindByIdentity(PC,IdentityType.UserPrincipalName,通过UserPrincipalName);
}
code工作正常时,code上机,这是在域中运行,但如果我尝试连接到AD机器是远程的,然后我得到错误 我试图用
字符串域=192.168.0.150:389/domain.com.pk;
那么它没有工作和验证凭证的方法总是返回false能有一个人帮助我如何我可以连接到使用IP与端口远程Active Directory PrincipalContext 或我必须使用目录条目
任何帮助将AP preciated
首先要注意:
code正常工作的code上机,这是在域中运行时
在这种情况下,您不需要提供管理用户+ PW在 PrincipalContext
的构造函数,如果机器是域成员(我假设在这里)。
如果你想连接到任何其他AD服务器(域控制器)与外部域和当前域之间没有信任,使用IP地址或服务器名称为域名称:
字符串域=192.168.0.150;
如果你的目标是只检查,如果凭据有效,你甚至可以忽略管理员用户+ PW:
字符串domainController =192.168.0.150;
使用(PrincipalContext PC =新PrincipalContext(ContextType.Domain,domainController))
{
字符串的UserPrincipalName =dotnettest+@+域;
//验证凭据
BOOL的isValid = pc.ValidateCredentials(UserPrincipalName进行,Ascertia 12);
}
在这种情况下,但是,你不能有
UserPrincipal起来= UserPrincipal.FindByIdentity(...
因为PrincipalContext本身没有登录。
您还可以看到我在一个类似的问题的答案:<一href="http://stackoverflow.com/a/28690682/4547223">http://stackoverflow.com/a/28690682/4547223
或此SO文章<一个href="http://stackoverflow.com/questions/290548/validate-a-username-and-password-against-active-directory">Validate用户名和密码对Active Directory?
i m trying to connect to Active Directory code that i have used
string domain = "domain.com.pk";
string container = "DC=mycompnay,DC=com,DC=pk";
string Admin = "salman.zafar";
string Password = "password";
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain, container, Admin, Password))
{
string userPrincipalName = "dotnettest" + "@" + domain;
// validate the credentials
bool isValid = pc.ValidateCredentials(userPrincipalName, "Ascertia 12");
if (isValid) {
UserPrincipal up = UserPrincipal.FindByIdentity(pc, IdentityType.UserPrincipalName, userPrincipalName);
}
code works fine when the code running on machine which is in domain but if i try to connect to the AD machine that is remote then i get error i tried to use
string domain = "192.168.0.150:389/domain.com.pk";
then it didn't work and validate credentials method always return false can some one help me how can i connect to remote active directory using IP with port with PrincipalContext or i have to use directory entry
any help will be appreciated
First note:
code works fine when the code running on machine which is in domain
In this case, you do not need to provide adminuser+pw in the PrincipalContext
constructor if the machine is a domain member (which I assume here).
If you want to connect to any other AD server (domain controller) with no trust between the foreign domain and the current domain, use the IP address or server name as the "domain" name:
string domain = "192.168.0.150";
If your goal is to just check if credentials are valid, you can even omit the admin user + pw:
string domainController = "192.168.0.150";
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainController))
{
string userPrincipalName = "dotnettest" + "@" + domain;
// validate the credentials
bool isValid = pc.ValidateCredentials(userPrincipalName, "Ascertia 12");
}
In this case, however, you cannot have
UserPrincipal up = UserPrincipal.FindByIdentity(...
because the PrincipalContext itself is not logged on.
You can also see my answer in a similar question: http://stackoverflow.com/a/28690682/4547223
or this SO article Validate a username and password against Active Directory?
这篇关于使用C#连接活动目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!