public class DirectoryHelper
{
public static string DomainName = System.Environment.UserDomainName;
public static string strLDAP = "LDAP://" + DomainName;
static DirectoryEntry de = new DirectoryEntry(strLDAP);
/// <summary>
/// 根据登录名获取用户域fullname
/// </summary>
/// <param name="AccountName"></param>
/// <returns></returns>
public static string getFullNameByAccountName(string AccountName)
{
string FullName = string.Empty;
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = string.Format("(SAMAccountName={0})", AccountName);
using (HostingEnvironment.Impersonate())
{
foreach (SearchResult sr in ds.FindAll())
{
string fullName = sr.GetDirectoryEntry().Name.ToString();
//if (sr.GetDirectoryEntry().Properties["samaccountname"].Value.ToString().ToLower() == AccountName)
//{
FullName = fullName.Substring(, fullName.Length - );
// }
}
}
return FullName;
} /// <summary>
/// 获取所有用户
/// </summary>
/// <returns></returns>
public static DataTable getAllPeople()
{
DataTable dt = new DataTable();
DataColumn dc_accountName = new DataColumn("AccountName", typeof(string));
DataColumn dc_mail = new DataColumn("Mail", typeof(string));
DataColumn dc_fullName = new DataColumn("FullName", typeof(string));
dt.Columns.Add(dc_fullName);
dt.Columns.Add(dc_accountName);
dt.Columns.Add(dc_mail);
DirectorySearcher search = new DirectorySearcher(de);
search.Filter = "(&(objectClass=user))";
search.SearchScope = SearchScope.Subtree;
//模拟用户登录(发布的时候不添加要报错)
using (HostingEnvironment.Impersonate())
{
SearchResultCollection SearchResults = search.FindAll();
if (SearchResults.Count > )
{
foreach (SearchResult sr in SearchResults)
{
DirectoryEntry GroupEntry = sr.GetDirectoryEntry();
string accountName = string.Empty;
string fullName = string.Empty;
string mail = string.Empty;
DataRow dr = dt.NewRow();
//先获取邮件属性,如果邮件不是空,说明是要取的部门
if (GroupEntry.Properties.Contains("mail"))
{
mail = GroupEntry.Properties["mail"][].ToString();
dr["Mail"] = mail;
if (GroupEntry.Properties.Contains("SAMAccountName"))
{
accountName = GroupEntry.Properties["SAMAccountName"][].ToString();
dr["AccountName"] = accountName;
}
if (GroupEntry.Properties.Contains("Name"))
{
fullName = GroupEntry.Properties["Name"][].ToString();
dr["FullName"] = fullName;
}
dt.Rows.Add(dr);
}
}
}
}
return dt;
} /// <summary>
/// 根据全名获取登录名
/// </summary>
/// <param name="fullName"></param>
/// <returns></returns>
public static string getAccountNameByFullName(string fullName)
{
string accountName = string.Empty;
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = string.Format("(cn={0})", fullName);
using (HostingEnvironment.Impersonate())
{
SearchResult sr = ds.FindOne();
if (sr != null)
{
if (sr.GetDirectoryEntry().Properties.Contains("samaccountname"))
{
accountName = sr.Properties["samaccountname"][].ToString(); //accountName = accountName.Substring(3, accountName.Length - 3);
}
}
}
return accountName;
} /// <summary>
/// 判断用户是否存在
/// </summary>
/// <param name="FullName"></param>
/// <returns></returns>
public static bool checkUserExist(string FullName)
{
bool Exist = false;
DirectorySearcher deSearch = new DirectorySearcher(de);
deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + FullName + "))";
deSearch.SearchScope = SearchScope.Subtree;
//模拟用户登录(发布的时候不添加要报错)
using (HostingEnvironment.Impersonate())
{
SearchResult result = deSearch.FindOne();
if (result != null)
{
Exist = true;
}
return Exist;
}
} #region 根据登录名获取用户所在组
public static DataTable getGroupByAccountName(string AccountName)
{
DataTable dt = new DataTable("group");
DataColumn groupName = new DataColumn("Name", typeof(string));
dt.Columns.Add(groupName); DirectorySearcher search = new DirectorySearcher(de);
search.Filter = "(&(sAMAccountName=" + AccountName + "))";
search.PropertiesToLoad.Add("memberof");
using (HostingEnvironment.Impersonate())
{
SearchResult result = search.FindOne();
if (result == null)
{
DataRow dr = dt.NewRow();
dr["Name"] = "暂无数据";
dt.Rows.Add(dr);
}
else
{
string[] results = new string[result.Properties["memberof"].Count + ];
for (int i = ; i < result.Properties["memberof"].Count; i++)
{
string theGroupPath = result.Properties["memberof"][i].ToString();
string tempName = theGroupPath.Substring(, theGroupPath.IndexOf(",") - );
if (tempName == "全体员工" || tempName == "VPN"||tempName=="经理级人员") { }
else
{
DataRow dr = dt.NewRow();
dr["Name"] = theGroupPath.Substring(, theGroupPath.IndexOf(",") - );
dt.Rows.Add(dr);
}
}
}
} return dt;
}
#endregion #region 获取it部门成员
public static DataTable getAllMembersByGroupName(string GroupName)
{
DataTable dt_ItMembers = new DataTable();
DataColumn dc_ItMembers = new DataColumn("Name", typeof(string));
dt_ItMembers.Columns.Add(dc_ItMembers); DirectorySearcher search_ItMember = new DirectorySearcher(de);
search_ItMember.Filter = "(&(objectClass=group)(cn=" + GroupName + "))";
//模拟用户登录(发布的时候不添加要报错)
using (HostingEnvironment.Impersonate())
{
SearchResult search_It = search_ItMember.FindOne();
if (search_It != null)
{
int memberCount = search_It.Properties["member"].Count;
for (int i = ; i < memberCount; i++)
{
if (!string.IsNullOrEmpty(search_It.Properties["member"][i].ToString()))
{
string name = search_It.Properties["member"][i].ToString();
name = name.Substring(, name.IndexOf(",") - );
DataRow dr_itMembersName = dt_ItMembers.NewRow();
dr_itMembersName["Name"] = name;
dt_ItMembers.Rows.Add(dr_itMembersName);
}
}
}
else
{
DataRow dr_none = dt_ItMembers.NewRow();
dr_none["Name"] = "暂无数据";
dt_ItMembers.Rows.Add(dr_none);
}
}
return dt_ItMembers;
}
#endregion #region 获取所有部门
public static DataTable getAllGroup()
{
DataTable dt_group = new DataTable();
DataColumn dc_group = new DataColumn("GroupName", typeof(string));
dt_group.Columns.Add(dc_group); DirectorySearcher search_ItMember = new DirectorySearcher(de);
search_ItMember.Filter = "(&(objectClass=group))";
search_ItMember.SearchScope = SearchScope.Subtree;
//模拟用户登录(发布的时候不添加要报错)
using (HostingEnvironment.Impersonate())
{
SearchResultCollection SearchResults = search_ItMember.FindAll();
if (SearchResults.Count > )
{
foreach (SearchResult sr in SearchResults)
{
DirectoryEntry GroupEntry = sr.GetDirectoryEntry();
//先获取邮件属性,如果邮件不是空,说明是要取的部门
if (GroupEntry.Properties.Contains("mail"))
{
string mail = GroupEntry.Properties["mail"][].ToString();
if (!string.IsNullOrEmpty(mail))
{
string groupName = string.Empty;
if (GroupEntry.Properties.Contains("Name"))
{
groupName = GroupEntry.Properties["Name"][].ToString();
}
DataRow dr_group = dt_group.NewRow();
dr_group["GroupName"] = groupName;
dt_group.Rows.Add(dr_group);
}
}
}
}
}
return dt_group;
}
#endregion #region 根据全名获取邮箱
public static string getMailByFullName(string fullName)
{
string mail = string.Empty;
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "(&(cn=" + fullName + "))";
using (HostingEnvironment.Impersonate())
{
SearchResult result = ds.FindOne();
if (result != null)
{
mail = result.Properties["mail"][].ToString();
}
return mail;
}
}
#endregion /// <summary>
/// 判断是不是IT部门的人
/// </summary>
/// <param name="AccountName"></param>
/// <returns></returns>
public static bool IsItGroup(string AccountName)
{
bool IsItGroupMember = false; DirectorySearcher search = new DirectorySearcher(de);
search.Filter = "(&(sAMAccountName=" + AccountName + "))";
search.PropertiesToLoad.Add("memberof");
using (HostingEnvironment.Impersonate())
{
SearchResult result = search.FindOne();
if (result != null)
{
for (int i = ; i < result.Properties["memberof"].Count; i++)
{
string theGroupPath = result.Properties["memberof"][i].ToString();
if (theGroupPath.Substring(, theGroupPath.IndexOf(",") - ) == "IT")
{
IsItGroupMember = true; return IsItGroupMember;
}
}
}
}
return IsItGroupMember;
}
}

调用:

 //获取当前登录电脑的用户名
string name = System.Environment.UserName.ToLower();
string fullname= DirectoryHelper.getFullNameByAccountName(System.Environment.UserName.ToLower());
bool b_ITGroupMember = DirectoryHelper.IsItGroup(name);

  

发布到IIS上后要修改IIS配置

ASP.NET impersonation和window authentication设置成启用,

其他的Annoymous authentication都设置成禁用

应用程序池设置成经典

DirectoryEntry 活动目录的使用的更多相关文章

  1. c#活动目录操作

    c#活动目录操作  https://www.cnblogs.com/ahuo/archive/2007/03/16/676853.html 添加引用 System.DirectoryServices导 ...

  2. Step by step 活动目录中添加一个子域

    原创地址:http://www.cnblogs.com/jfzhu/p/4006545.html 转载请注明出处 前面介绍过如何创建一个域,下面再介绍一下如何在该父域中添加一个子域. 活动目录中的森林 ...

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

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

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

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

  5. sql server 2008 在安装了活动目录以后无法启动服务了

    软件环境: windows server 2008 r2 ms sql server 2008 r2 在安装活动目录以前,数据库是正常运行的. 安装了活动目录以后,数据库启动时就提示无法启动.出错的信 ...

  6. Delphi访问活动目录

    活动目录Active Directory是用于Windows Server的目录服务,它存储着网络上各种对象的有关信息,并使该信息易于管理员和用户查找及使用.Active Directory使用结构化 ...

  7. windows server 2012 AD 活动目录部署系列(五)备份和还原域控制器

    在前篇博文中,我们介绍了用户资源的权限分配,用户只要在登录时输入一次口令,就能访问基于该域所分配给他的所有资源. 但是我们需要考虑一个问题:万一域控制器坏了怎么办?!如果这个域控制器损坏了,那用户登录 ...

  8. 网络操作系统 第六章 Window Server 2008 活动目录

    本章小结 在企业网络中,活动目录是必不可少的,它将网络中各种对象组织起来进行管理,方便了网络对象的查找,增强了网络的安全性,为用户的物流管理提供了极大的便利,本章首先介绍了活动目录的基本概念,然后通过 ...

  9. windows 活动目录双向信任配置

    活动目录A:ess.com 192.168.1.20/24 活动目录B:ups.com  192.168.1.30/24 Step1:在活动目录B的域控制器上配置域的林双向信任关系,并且可传递,如下图 ...

随机推荐

  1. CF Codeforces Round #231 (Div. 2)

    http://codeforces.com/contest/394 话说这次CF做的超级不爽,A题一开始交过了,我就没再管,B题还没看完呢,就死困死困的,后来觉得B题枚举一下估计能行,当时是觉得可以从 ...

  2. C语言中随机数的生成

    刚好在找这方面的资料,看到了一片不错的,就全文转过来了,省的我以后再找找不到. 在C语言中,可以通过rand函数得到一个“伪随机数”.这个数是一个整数,其值大于等于0且小于等于RAND_MAX.ran ...

  3. Little-endian的一个好处:在变量指针转换的时候地址保持不变

    Big-endian 的内存顺序和数字的书写顺序是一致的,方便阅读理解.Little-endian 在变量指针转换的时候地址保持不变,比如 int64* 转到 int32* 各有利弊,统一就好,目前看 ...

  4. 转:HTTP请求(GET、POST和soap区别)和响应

    一直对Http请求和SOAP请求不是太理解,只是知道SOAP是基于Http的,并且增加了很多XML标签,SOAP经常用在WebService中,比如在C#中创建一个WebService,然后在客户端生 ...

  5. 【HDOJ】3509 Buge's Fibonacci Number Problem

    快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. /* 3509 */ #inc ...

  6. bzoj3130

    这道题要谈很多: 首先,第一问等会我另外说一下: 第二问比较难想,首先我们的考虑两人的最优策略是什么 对于Bob,我们令分配了x条边的费用,则我们要最大化 ans=Σ(i=1 to x) flow[i ...

  7. [HDU POJ] 逆序数

    HDU 1394 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3276 ...

  8. MVC 路由规则

    一.路由规则说明 先来看MVC中的默认路由的说明 "{controller}/{action}/{id}", // URL with parameters 对于Url /Home/ ...

  9. 解决eclipse闪退的办法

    Eclipse Java EE 便出现打开闪退的现象. 修改eclipse.ini无效,参照了网上许多方法也不行. 最后发现了一个蛋疼的方法: 1.打开eclipse的workspace目录 2.删除 ...

  10. HDU-1238 Substrings

    Substrings Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...