直接上干货,核心代码如下,读取出来相应的用户清单到DataTable中。需要其它字段可以自己增加,别忘了引用using System.DirectoryServices。

 #region private DataTable GetData(int pageIndex, int pageSize, out int recordCount) 获取数据

     /// <summary>
/// 获取数据
/// </summary>
private DataTable GetData(int pageIndex, int pageSize, out int recordCount)
{
string ldapPath = this.txtLDAPPath.Text.Trim();
string ldapDomain = this.txtLDAPDomain.Text.Trim();
string ldapUserName = this.txtLDAPUserName.Text.Trim();
string ldapPassword = this.txtLDAPPassword.Text.Trim();
string searchValue = this.txtSearch.Text.Trim(); //总记录数量
int iRecordCount = ;
//BaseUserManager manager = new BaseUserManager(this.UserCenterDbHelper, this.UserInfo);
//自定义LDAP用户表
DataTable dtLdapUser = null;
dtLdapUser = new DataTable();
DataColumn[] columns =
{
new DataColumn(BaseUserEntity.FieldId),
new DataColumn(BaseUserEntity.FieldUserName),
new DataColumn(BaseUserEntity.FieldRealName),
new DataColumn(BaseUserEntity.FieldUserFrom)
};
dtLdapUser.Columns.AddRange(columns);
try
{
// 读取用户
using (var de = new DirectoryEntry())
{
de.Path = ldapPath;
if (!string.IsNullOrEmpty(ldapUserName))
{
if (string.IsNullOrEmpty(ldapDomain))
{
de.Username = ldapUserName;
}
else
{
de.Username = ldapDomain + "\\" + ldapUserName;
}
}
//密码为空就默认不验证用户密码
if (!string.IsNullOrEmpty(ldapPassword))
{
de.Password = ldapPassword;
de.AuthenticationType = AuthenticationTypes.Secure;
}
//刷新缓存
de.RefreshCache(); using (DirectorySearcher searcher = new DirectorySearcher())
{
searcher.SearchRoot = de;
searcher.Filter = "(objectClass=user)";
searcher.SearchScope = SearchScope.Subtree; //需要导入的属性
//登录名
searcher.PropertiesToLoad.Add("userprincipalname");
//姓名
searcher.PropertiesToLoad.Add("name"); //定义排序方式
searcher.Sort = new SortOption("givenName", System.DirectoryServices.SortDirection.Ascending); SearchResultCollection results = searcher.FindAll();
int i = ;
foreach (SearchResult result in results)
{
DataRow row = dtLdapUser.NewRow(); ResultPropertyCollection props = result.Properties;
string[] userNameArray = null;
if (props.PropertyNames != null)
{
foreach (string propName in props.PropertyNames)
{
if (propName == "userprincipalname")
{
userNameArray = props[propName][].ToString().Split('@');
//只保留@前的用户名
row[BaseUserEntity.FieldUserName] = userNameArray[].ToString();
}
if (propName == "name")
{
row[BaseUserEntity.FieldRealName] = props[propName][];
}
}
}
row[BaseUserEntity.FieldUserFrom] = "AD";
if (userNameArray != null && !string.IsNullOrEmpty(userNameArray[].ToString()))
{
i++;
row[BaseUserEntity.FieldId] = i;
dtLdapUser.Rows.Add(row);
}
}
}
} recordCount = dtLdapUser.Rows.Count; }
catch (Exception e)
{
recordCount = ;
}
return dtLdapUser;
}
#endregion

我还增加了LDAP(Windows AD域)用户一键导入的功能,截图如下,代码核心就是循环读取上述获取的DataTable,记得判断一下用户是否已经存在即可。

C# Webform中读取Windows AD/LDAP域用户清单的更多相关文章

  1. java web 中 读取windows图标并显示

    java web中读取windows对应文件名的 系统图标 ....显示 1.获取系统图标工具类 package utils;  import java.awt.Graphics;  import j ...

  2. 在Windows Server 2008 R2中使用web方式修改域用户账户密码

    在Windows的domain环境下,加域的客户端修改账户密码是一件很easy的事情:即使没有加域的客户端如果组织中,使用Exchange邮件系统,借助Exchange的owa也可以轻松修改账户密码. ...

  3. WINDOWS 2008 SERVER域用户自动登陆

    The user I wanted to auto-logon as didn’t have a password, this reg hack worked instead: HKEY_LOCAL_ ...

  4. Django开发文档-域用户集成登录

    项目概述: 一般在企业中,用户以WINDOWS的域用户统一的管理,所以以Django快速开发的应用,不得不集成AD域登录. 网上一般采用django-python3-ldap的库来做集成登录,但是本方 ...

  5. Window通过Web方式修改域用户密码

    如何通过web方式修改域用户密码: 1.在Windows Server 2003上,系统默认提供了iisadmpwd作为一种修改域用户密码的方式 2.在Windows Server 2008上,可以提 ...

  6. SharePoint 2010中重置windows 活动目录(AD)域用户密码的WebPart(免费下载)

    由于SharePoint 2013推出不久,并非所有的企业都会升级到SharePoint 2013的,毕竟升级不是打打补丁这么简单,更多的企业还是使用Sharepoint 2010版本的,因此本人自行 ...

  7. SharePoint 2013中修改windows 活动目录(AD)域用户密码的WebPart(免费下载)

    前段时间工作很忙,好久没更新博客了,趁国庆休假期间,整理了两个之前积累很实用的企业集成组件,并在真正的大型项目中经受住了考验:.Net版SAP RFC适配器组件和SharePoint 2013修改AD ...

  8. Windows AD域升级方

    前面的博客中我谈到了网络的基本概念和网络参考模型,今天我们来谈企业中常用的技术,Windows AD 域,今天我的笔记将重点讲解Windows AD 域的升级和迁移方法,通过3个小实验进行配置,真实环 ...

  9. windows AD域安装及必要设置

    一.安装AD域 运行dcpromo命令,安装AD域. 步骤: 1.win+R 2.dcpromo 图例: 百度百科关于“dcpromo”解释: dcpromo命令是一个“开关”命令.如果Windows ...

随机推荐

  1. Cantor表 1083

    题目描述 Description 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 - 2/1 2/ ...

  2. 关于基本视频播放的Demo

    最近在做一个视频的Demo,当然是仿的别人的,现贴出原文地址:http://code4app.com/forum.php?mod=viewthread&tid=8959&highlig ...

  3. Openlayers实现第一张地图

    <html><head><title>OpenLayers Hello World</title> <style type="text/ ...

  4. Spring简单属性注入--不常用!!!

    UserDAOImpl.java: package com.bjsxt.dao.impl; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.U ...

  5. MapReduce 简单的全文搜索2

    上一个全文搜索实现了模糊查找,这个主要实现了精确查找,就是比如你查找mapreduce is simple那么他就只查找有这个句子的文章,而不是查找有这三个单词的文章. 这个版本需要重写反向索引,因为 ...

  6. 【转】C\C++代码优化的27个建议

    1. 记住阿姆达尔定律: funccost是函数func运行时间百分比,funcspeedup是你优化函数的运行的系数. 所以,如果你优化了函数TriangleIntersect执行40%的运行时间, ...

  7. List学习笔记

    List 特点:1.有序.2.可重复. ArrayList: 底层是数组,数组是有下标的. 会自动扩容,底层默认初始化容量是10,扩大之后的容量预设是原来容量的一半(jdk 1.8).以前好像是原容量 ...

  8. svn无法提交

    svn无法提交, 错误信息:Commit failed. svn: E200007: CHECKOUT can only be performed on a version resource... 解 ...

  9. Zju1290 Word-Search Wonder(http://begin.lydsy.com/JudgeOnline/problem.php?id=2768)

    2768: Zju1290 Word-Search Wonder Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 4  Solved: 2[Submit] ...

  10. iOS 之 网络 NSURLSession

    NSURLSession相比NSURLConnection功能更强大,是更上层的网络封装.相比而言,普通应用场景下NSURLSession没有什么优势,但是,在程序切换到后台的情况下,NSURLSes ...