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
;
}
查看全文
相关阅读:
[MySQL] 数据库基本概念
[LeetCode] Number of 1 Bits
[LeetCode] Maximum Subarray
[LeetCode] Search Insert Position
[LeetCode] Remove Duplicates from Sorted List
[LeetCode] Path Sum III
[LeetCode] Not Boring Movies
[LeetCode] Swap Salary
[LeetCode] Big Countries
中国银联全渠道系统商户接入 测试指引-银联网关支付产品
原文地址:https://www.cnblogs.com/zyk/p/59707.html
最新文章
leetcode-8
leetcode-7
leetcode-6
基于队列的环实现
斐波那契的线性排序
排序-2
leetcode-5
排序-1
leetcode-4
php学习之try catch
热门文章
PHP 实现多服务器共享 SESSION 数据
PHP Memcached应用实现代码
IIS伪静态失效
php服务端接收post的json数据
使用json传输数组实例
php数组与字符串转换
php url地址栏传中文乱码解决方法集合
php 使用serialize() 和 unserialize() 让对象成超级变量
gbk编码文件传输json实例
[MySQL] 下载及登录MySQL
Copyright © 2011-2022 走看看