0

私はこの問題を解決するために何日もインターネットを検索してきました。

私は、ユーザーがASP.NETWebアプリケーションを使用してパスワードを変更できるようにする必要があるプロジェクトに取り組んでいます。

パスワード履歴を適用する必要があるため、「SetPassword」ではなく「ChangePassword」を使用する必要があります。また、LDS内のユーザーに必要以上の特権を許可しないようにする必要があります。私は開発環境でこのタスクを完了しようとしています。「Server1」(LDS、PingFederate、CA)と「Server2」(IIS)の2台のマシンがあります。2つのボックスの間にSSLが設定されていなかったため、問題が発生する可能性があると考えたため、昨日半日かけてCAを設定し、両方のマシンの証明書を作成しました。エラーログにエラーが表示されなくなったため、動作していると確信しています。SSLをオンにしたポート636を使用してLDPを使用してLDSにログインできます。また、これらのマシンはドメイン環境にありません。テストネットワーク上のすべてのマシンでhostsファイルを編集しました。

私はさまざまなバリエーションのコードを試しました:

public static bool ChangePassword(string email, string pwd, string newPwd)
{
    DirectoryEntry user  = GetCN(email);
    string username = user.Properties["cn"][0].ToString();

    DirectoryEntry de = new DirectoryEntry();
    de.AuthenticationType = AuthenticationTypes.Secure | AuthenticationTypes.SecureSocketsLayer;
    de.Path = user.Path;
    de.Username = username;
    de.Password = pwd;
    try
    {
        Object obj = de.NativeObject;
        de.Options.PasswordEncoding = PasswordEncodingMethod.PasswordEncodingSsl;
        de.Options.PasswordPort = Convert.ToInt32(ConfigurationManager.AppSettings["LDAPPort_ExternalUsers"]);
        de.Invoke("ChangePassword", new object[] { pwd, newPwd });
        de.CommitChanges();
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

public static bool ChangePassword(string email, string pwd, string newPwd)
{
    DirectoryEntry user  = GetCN(email);
    string username = user.Properties["cn"][0].ToString();

    DirectoryEntry de = new DirectoryEntry(user.Path, username, pwd, AuthenticationTypes.Secure | AuthenticationTypes.SecureSocketsLayer);
    try
    {
        de.Options.PasswordEncoding = PasswordEncodingMethod.PasswordEncodingSsl;
        de.Options.PasswordPort = Convert.ToInt32(ConfigurationManager.AppSettings["LDAPPort_ExternalUsers"]);
        de.Invoke("ChangePassword", new object[] { pwd, newPwd });
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

public static bool ChangePassword(string email, string pwd, string newPwd)
{
    DirectoryEntry userInc  = GetCN(email);
    string username = userInc.Properties["cn"][0].ToString();

    using (DirectoryEntry searchRoot = new DirectoryEntry(ConfigurationManager.AppSettings["LDAPConnectionString_ExternalUsers"], username, pwd, AuthenticationTypes.SecureSocketsLayer | AuthenticationTypes.Secure))
    using (DirectorySearcher ds = new DirectorySearcher(searchRoot))
    {
        ds.Filter = "(|(objectCategory=user)(cn=" + username + "))";
        SearchResult sr = ds.FindOne();
        if (sr != null)
        {
            using (DirectoryEntry user = sr.GetDirectoryEntry())
            {
                user.Options.PasswordEncoding = PasswordEncodingMethod.PasswordEncodingClear;
                user.Options.PasswordPort = Convert.ToInt32(ConfigurationManager.AppSettings["LDAPPort_ExternalUsers"]);
                //user.Invoke("SetOption", new object[] { 6, Convert.ToInt32(ConfigurationManager.AppSettings["LDAPPort_ExternalUsers"]) });
                //user.Invoke("SetOption", new object[] { 7, 1 });
                user.Invoke("ChangePassword", new object[] { pwd, newPwd });
            }
            return true;
        }
    }
    return false;
}

Object obj=de.NativeObject;の最初のバージョンで例外が発生します。これを使用してバインドが正しく行われているかどうかを判断し、これがポート389を介してユーザーを認証する方法であるため、デバッグ手順として挿入されました。例外は「ログオンの失敗:不明なユーザー名または不正なパスワード」です。

2番目のバージョンでde.Options.PasswordEncoding=PasswordEncodingMethod.PasswordEncodingSsl;で例外が発生します。例外は「ログオンの失敗:不明なユーザー名または不正なパスワード」です。

SearchResult sr = ds.FindOne();の3番目のバージョンで例外が発生します。例外は「ログオンの失敗:不明なユーザー名または不正なパスワード」です。

AuthenticatioTypes.Noneを使用してポート389でこのコードを実行しようとした場合| AuthenticationTypes.FastBind、de.Invoke( "ChangePassword"、new object [] {pwd、newPwd});で失敗します。「不明な名前」を除いて。これをSSLで実行するか、少なくともパスワードを平文で送信しないようにしたいと思います。HTTPS経由でサイトを実行しています。非SSL接続でパスワードを変更できるようにLDSのdsHeuristics値を変更しようとしましたが、それも機能しませんでした。

誰かが持っているかもしれないどんな提案でも大いに感謝されるでしょう。

4

1 に答える 1

0

ようやくパスワード変更を機能させることができました。http://www.informit.com/articles/article.aspx?p=474649&seqNum=4に記載されている情報を使用しました

私はこれらの3つの関数を使用しました:

private static DirectoryConnection GetConnection(string server, NetworkCredential credential, bool useSsl)
{
LdapConnection connection =
  new LdapConnection(server);

     if (useSsl)
     {
          connection.SessionOptions.SecureSocketLayer = true;
     }
     else
     {
          connection.SessionOptions.Sealing = true;
     }

     connection.Bind(credential);
     return connection;
}

private static void ChangePassword(DirectoryConnection connection, string userDN, string oldPassword, string newPassword)
{
     DirectoryAttributeModification deleteMod = new DirectoryAttributeModification();
     deleteMod.Name = "unicodePwd";
     deleteMod.Add(GetPasswordData(oldPassword));
     deleteMod.Operation= DirectoryAttributeOperation.Delete;

     DirectoryAttributeModification addMod = new DirectoryAttributeModification();
     addMod.Name = "unicodePwd";
     addMod.Add(GetPasswordData(newPassword));
     addMod.Operation = DirectoryAttributeOperation.Add;

     ModifyRequest request = new ModifyRequest(userDN, deleteMod, addMod);

     DirectoryResponse response = connection.SendRequest(request);
}

private static byte[] GetPasswordData(string password)
{
     string formattedPassword;
     formattedPassword = String.Format("\"{0}\"", password);
     return (Encoding.Unicode.GetBytes(formattedPassword));
}

パスワードの変更を許可するページの後ろのコードから、このような関数を呼び出しました。

NetworkCredential credential = new NetworkCredential("[user with access to the LDS", "pwd");
            DirectoryConnection connection;
            try
            {
                //Setup our connection
                connection = ADAuth.GetConnection(ConfigurationManager.AppSettings["LDAPServer_ExternalUsers"] + ":" + ConfigurationManager.AppSettings["LDAPPort_ExternalUsers"], credential, true);

                //Attempt to change the password
                ADAuth.ChangePassword(connection, ADAuth.GetDN(userID.Text).Properties["distinguishedName"].Value.ToString(), currPass.Text, newPass.Text);

                //Send success message to user
                ErrorLit.Text = "<p>Password change successful!</p>";
                //Dispose the connection
                IDisposable disposable = connection as IDisposable;
                if (disposable != null)
                    disposable.Dispose();
            }
            catch (Exception ex)
            {
                //There was an error, tell the user
                errors += "<li>Error changing password</li>";
                ErrorLit.Text = errors + "</ul>";
                return;
            }
于 2011-07-26T17:05:26.077 に答える