zoukankan
html css js c++ java
基于深度优先的递归判断域用户是否是某个组的成员
基于深度优先的递归判断域用户是否是某个组的成员
引用System.DirectoryServices
并导入名称空间
using
System.DirectoryServices;
功能:判断域用户(登录名)是否是某个域安全组的成员,域用户可能属于多个组,并且所属的组可能又属于多个组,所以需要递归调用.
private
DirectoryEntry entry
=
new
DirectoryEntry(
"
LDAP://domain
"
,
@"
domain\username
"
,
"
password
"
);
private
bool
UserisGroupMember(
string
UserLogin,
string
RoleName)
{
DirectorySearcher mySearcher
=
new
DirectorySearcher(entry);
mySearcher.Filter
=
string
.Format (
"
(&(objectClass=user)(sAMAccountName={0}))
"
,UserLogin );
mySearcher.PropertiesToLoad.Add (
"
memberof
"
);
SearchResult mysr
=
mySearcher.FindOne();
if
(mysr.Properties .Count
>
1
)
//
返回两个属性,一个是内置的adspath,另一个是PropertiesToLoad加载的
{
string
[] memberof
=
new
string
[mysr.Properties[
"
memberof
"
].Count ];
int
i
=
0
;
foreach
( Object myColl
in
mysr.Properties[
"
memberof
"
])
{
memberof[i]
=
myColl.ToString ().Substring (
3
,myColl.ToString ().IndexOf(
"
,
"
)
-
3
);
if
(memberof[i]
==
RoleName)
return
true
;
i
++
;
}//其实这一层循环是广度优先算法,因为考虑到一个人直接属于某个安全组的可能性要大一些,这样做效率更高.如果把下面这个循环放到上面的if的esle中,就是完全的深度优先了.
foreach
(
string
GroupName
in
memberof)
if
(MemberisGroupMember(GroupName,RoleName))
return
true
;
}
return
false
;
}
private
bool
MemberisGroupMember(
string
GroupName,
string
RoleName)
{
DirectorySearcher mySearcher
=
new
DirectorySearcher(entry);
mySearcher.Filter
=
string
.Format (
"
(&(objectClass=group)(CN={0}))
"
,GroupName );
mySearcher.PropertiesToLoad.Add (
"
memberof
"
);
SearchResult mysr
=
mySearcher.FindOne();
string
memberof;
if
(mysr.Properties.Count
>
1
)
//
返回两个属性,一个是内置的adspath,另一个是PropertiesToLoad加载的
{
foreach
( Object myColl
in
mysr.Properties[
"
memberof
"
])
{
memberof
=
myColl.ToString ().Substring (
3
,myColl.ToString ().IndexOf(
"
,
"
)
-
3
);
if
(memberof
==
RoleName)
return
true
;
else
if
(MemberisGroupMember(memberof,RoleName))
return
true
;
}
}
return
false
;
}
查看全文
相关阅读:
android studio 开发环境配置
解决 php-cgi 启动时提示缺少 msvcr110.dll 的问题
PHP5.5 + IIS + Win7的配置
PHP版本VC6和VC9、Non Thread Safe和Thread Safe的区别
[6]Telerik TreeView 复选框
详解.NET IL代码(一)
第三章 续:时间控件(TimePicker)
第二章 时间控件(DateTime Picker)
jquery常用方法
RESTClient
原文地址:https://www.cnblogs.com/zyk/p/59707.html
最新文章
Creating, Stopping, Re-Starting and Deleting a Timer in Oracle Forms
win7x64安装wince6
项目经理和产品经理
wince6下载地址
聊聊移动端跨平台开发的几种流派
聊聊移动端跨平台开发的各种技术
使用C++读写Excel
2016年4月TIOBE编程语言排行榜 Visual Basic正渐行渐远
Python GUI库
从程序员到软件设计师
热门文章
产品经理 产品设计师常用软件
艾瑞克·弗洛姆 ( Erich Fromm )
弗洛伊德学说中的本我、自我和超我
Linux Wireless Supported Devices
The Art Of Loving
WSL 服务自动启动
LunHui 的生命观
Android Studio 生成 keystore 签名文件
在ubuntu 16.04 的vm中添加新网卡,同一网段不同ip
Ubuntu install android studio
Copyright © 2011-2022 走看看