簡體   English   中英

C#通過sAMAccount查找活動目錄用戶

[英]c# finding active directory user by sAMAccount

.NET 4.0的新手。
我們有一個腳本,該腳本當前使用以下代碼重置用戶密碼:

 DirectoryEntry de = new DirectoryEntry("myLdapString");

 DirectoryEntry ChgPwd = de.Children.Find("CN=" + "myuserid", "user");

 ChgPwd.Invoke("SetPassword", new object[] { "newPWD" });

 ChgPwd.CommitChanges();

我要更改此名稱,以便使用“ sAMAccount =“而不是“ CN =””指向用戶。 但是,在上面的“查找”字符串中更改該名稱將無效。 有人可以提供有關此更改的正確語法的幫助嗎? 謝謝

您可以使用System.DirectoryServices.AccountManagement命名空間來管理有效的目錄帳戶。

像這樣的代碼

using(PrincipalContext principalContext = new PrincipalContext( ContextType.Domain,
            TargetDomain,
            TargetDomainUserName,
            TargetDomainPassword))
 using(var userPrincipal = UserPrincipal.FindByIdentity(principalContext, IdentityType.SamAccountName, "somaloginname"))
{ 
userPrincipal.SetPassword(newPassword);
//or userPrincipal.ChangePassword
            userPrincipal.Save();
            }

MSDN:UserPrincipal類

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM