C#操作域的第一步便是测试程序是否能成功连接到域,通过LDAP协议,可以非常简单的测试域连接的情况。代码如下
/// </summary>
/// <param name="domainName">域名或IP</param>
/// <param name="userName">用户名</param>
/// <param name="userPwd">密码</param>
/// <param name="entry">域</param>
/// <returns></returns>
public string IsConnect(string domainName, string userName, string userPwd, out DirectoryEntry domain)
{
domain = new DirectoryEntry();
try
{
domain.Path = string.Format("LDAP://{0}", domainName);//LDAP是轻量目录访问协议
domain.Username = userName;
domain.Password = userPwd;
domain.AuthenticationType = AuthenticationTypes.Secure;//身份验证的类型
domain.RefreshCache();//将此DirectoryEntry 对象的属性值加载到属性缓存中
return "测试连接成功!";
}
catch (Exception ex)
{
return "[IsConnected方法]错误信息:" + ex.Message;
}
}
呵呵
博客大好,让人忘不了!
感谢分享