0

.NET 3.5 で導入された名前空間を使用してAccountManagement、ユーザーを検索し、パスワードを設定したいと考えています。ただし、ADLDS サーバーは当社のドメインの一部ではないため、使用していContextType.Machineます。ユーザーを検索しても見つかりません(間違ったコンテナーで検索していると思われますが、使用時のドキュメントによると、ContextType.Machineコンテナーを指定できません)。

using (var context = new PrincipalContext(ContextType.Machine, "test-server", null, "username", "password")) {
    using (var u = UserPrincipal.FindByIdentity(context, "SuperAdmin")) {
        //u is always null.   :(
    }
}

ただし、単純な ol' を使用してユーザーを見つけることができることはわかっていますDirectoryEntry

using (var de = new DirectoryEntry("LDAP://test-server:389/CN=SuperAdmin,CN=SuperUsers,OU=test-server,DC=foo,DC=bar,DC=com", "username", "password", AuthenticationTypes.Secure)) {
    //The user is found, but SetPassword fails with "The directory property cannot be found in the cache"
    de.Invoke("SetPassword", new object[] { "foobar" });
}

最後に指摘しておきたいのは、ADSI Edit を使用して、同じ資格情報でパスワードを変更できることです。新しいディレクトリ オブジェクトを使用してこの検索を実行できますか?

4

1 に答える 1

0

それは本当に古い質問ですが、最近、同様のプロジェクトに取り組まなければなりませんでした...誰かが同じ問題に遭遇した場合は、回答を投稿します。

userusingクラスが見つからない理由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 を使用することをお勧めします。*

于 2015-07-06T23:31:59.030 に答える