それは本当に古い質問ですが、最近、同様のプロジェクトに取り組まなければなりませんでした...誰かが同じ問題に遭遇した場合は、回答を投稿します。
user
usingクラスが見つからない理由UserPrincipal
は、あなたが言及したように、を使用して検索しているためですContextType.Machine
。しかし、DirectEntry
クラスでは単純なLDAP://
クエリを実行しているだけです。
これが私の解決策です。
サーバー情報を.config
ファイルに保存します。
...
//Server name and port
<add key ="ADLDS_Server" value="Servername:port"/>
//Note* depending on structure container will be different for everybody
<add key ="ADLDS_Container" value="CN=Some Value, DC=some value,DC=value"/>
...
次に、オブジェクトADLDSUtility
を返すクラスを作成しました。PrincipalContext
...
using System.DirectoryServices.AccountManagement
...
public class ADLDSUtility
{
public static ContextOptions ContextOptions = ContextOptions.SecureSocketLayer | ContextOptions.Negotiate;
public static PrincipalContext Principal
{
get
{
return new PrincipalContext(
ContextType.ApplicationDirectory,
ConfigurationManager.AppSettings["ADLDS_Server"],
ConfigurationManager.AppSettings["ADLDS_Container"],
//you can specify username and password if need to
ContextOptions);
}
}
そこから、(username、currentPassword、および newPassword)method
をパラメーターとして受け入れるa を作成しました。
public void ChangePassword(string userName, string currentPassword, string newPassword)
{
using (PrincipalContext ctx = ADLDSUtility.Principal)
{
using (UserPrincipal principal = new UserPrincipal(ctx))
{
using (var searchUser = UserPrincipal.FindByIdentity(ctx, IdentityType.UserPrincipalName, userName))
{
if (searchUser != null)
{
searchUser.ChangePassword(currentPassword, newPassword);
// searchUser.SetPassword(newPassword);
if (String.IsNullOrEmpty(searchUser.Guid.ToString()))
{
throw new Exception("Could not change password");
}
}
}
}
}
}
この例では、 でユーザーを検索していますUserPrincipalName
。しかし、私たちはそれに限定されません。IdentityType.Guid
などでユーザーを検索することもできます。
現在searchUser
、パスワードを含む 2 つの方法があります。両方提供させていただきました。
//requires current and new password
searchUser.ChangePassword(currentPassword, newPassword);
//setting a password. Only requires new password.
searchUser.SetPassword(newPassword);
パスワードの設定または変更には SSL を使用することをお勧めします。*