啟動賬戶:
DirectoryEntry usr =
new
DirectoryEntry(
"
LDAP://CN=New User,CN=users,DC=fabrikam,DC=com
"
);
int
val = (
int
) usr.Properties[
"
userAccountControl
"
].Value;
usr.Properties[
"
userAccountControl
"
].Value = val & ~(
int
)ActiveDs.ADS_USER_FLAG.ADS_UF_ACCOUNTDISABLE;
//=544
usr.CommitChanges();
停用賬戶:
DirectoryEntry usr =
new
DirectoryEntry(
"
LDAP://CN=Old User,CN=users,DC=fabrikam,DC=com
"
);
int
val = (
int
) usr.Properties[
"
userAccountControl
"
].Value;
usr.Properties[
"
userAccountControl
"
].Value = val |
(
int
)ActiveDs.ADS_USER_FLAG.ADS_UF_ACCOUNTDISABLE;
//=546
usr.CommitChanges();
ActiveDs.ADS_USER_FLAG.ADS_UF_ACCOUNTDISABLE值需要引用庫才可使用;
引用COM組件: Active DS Type Library
---------------------------------------------------
?關于創建用戶主要碰到了兩個問題:
一、就是上面的啟動/停用的問題
二、就是密碼設置問題
創建用戶,使用usr.Properties["userPassword"].add("m12345.");設置密碼,密碼一直沒有設置成功,原因不詳[大概userPassword不是存儲密碼的吧...]。
之后改為 usr.Invoke("SetPassword","m12345.");就成功了.
修改密碼使用usr.Invoke("ChangePassword", new object[] { "old", "new" });
---------------------------------------------------
關于.net3.5之后的版本(應該吧)有一個更簡潔的方法創建用戶修改密碼等。
創建用戶:
using
(
var
context =
new
PrincipalContext(ContextType.Domain,
"
cninnovation
"
))
using
(
var
user =
new
UserPrincipal(context,
"
Tom
"
,
"
P@ssw0rd
"
,
true
)
{
GivenName
=
"
Tom
"
,
EmailAddress
=
"
test@test.com
"
})
{
user.Save();
}
重置密碼:
using
(
var
context =
new
PrincipalContext(ContextType.Domain,
"
cninnovation
"
))
using
(
var
user =
UserPrincipal.FindByIdentity(context, IdentityType.Name,
"
Tom
"
))
{
user.SetPassword(
"
Pa$$w0rd
"
);
user.Save();
}
創建組:
using
(
var
ctx =
new
PrincipalContext(ContextType.Domain,
"
cninnovation
"
))
using
(
var
group =
new
GroupPrincipal(ctx)
{
Description
=
"
Sample group
"
,
DisplayName
=
"
Wrox Authors
"
,
Name
=
"
WroxAuthors
"
})
{
group.Save();
}
組中添加用戶:
using
(
var
context =
new
PrincipalContext(ContextType.Domain))
using
(
var
group =
GroupPrincipal.FindByIdentity(context, IdentityType.Name,
"
WroxAuthors
"
))
using
(
var
user =
UserPrincipal.FindByIdentity(context, IdentityType.Name,
"
Stephanie Nagel
"
))
{
group.Members.Add(user);
group.Save();
}
查找用戶:
using
(
var
context =
new
PrincipalContext(ContextType.Domain,
"
explorer
"
))
using
(
var
users =
UserPrincipal.FindByPasswordSetTime(context, DateTime.Today
-TimeSpan.FromDays(
30
), MatchType.LessThan))
{
foreach
(
var
user
in
users)
{
Console.WriteLine(
"
{0}, last logon: {1},
"
+
"
last password change: {2}
"
, user.Name, user.LastLogon, user.LastPasswordSet);
}
}
?
var
context =
new
PrincipalContext(ContextType.Domain);
var
userFilter =
new
UserPrincipal(context);
userFilter.Surname
=
"
Nag*
"
;
userFilter.Enabled
=
true
;
using
(
var
searcher =
new
PrincipalSearcher())
{
searcher.QueryFilter
=
userFilter;
var
searchResult =
searcher.FindAll();
foreach
(
var
user
in
searchResult)
{
Console.WriteLine(user.Name);
}
}
?
?
參考資料: http://msdn.microsoft.com/zh-tw/library/ms180913(v=vs.90).aspx
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

