質問

Windows Vista Ultimate SP1 で次のコードを使用して、Active Directory サーバーにクエリを実行し、ドメイン上のユーザーのユーザー名とパスワードを確認しています。

public Object IsAuthenticated()
{
    String domainAndUsername = strDomain + "\\" + strUser;
    DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, strPass);
    SearchResult result;
    try
    {
        //Bind to the native AdsObject to force authentication.         

        DirectorySearcher search = new DirectorySearcher(entry) { Filter = ("(SAMAccountName=" + strUser + ")") };

        search.PropertiesToLoad.Add("givenName"); // First Name                
        search.PropertiesToLoad.Add("sn"); // Last Name
        search.PropertiesToLoad.Add("cn"); // Last Name

        result = search.FindOne();

        if (null == result)
        {
            return null;
        }

        //Update the new path to the user in the directory.
        _path = result.Path;
        _filterAttribute = (String)result.Properties["cn"][0];
    }
    catch (Exception ex)
    {
        return new Exception("Error authenticating user. " + ex.Message);
    }
    return user;
}

ターゲットは .NET 3.5 を使用し、VS 2008 標準でコンパイルされています

アプリケーションが実行されているドメイン管理者のドメイン アカウントでログインしています。

このコードは Windows XP 上で完全に動作します。しかし、Vista で実行すると次の例外が発生します。

System.DirectoryServices.DirectoryServicesCOMException (0x8007052E): Logon failure: unknown user name or bad password.

   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_AdsObject()
   at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
   at System.DirectoryServices.DirectorySearcher.FindOne()
   at Chain_Of_Custody.Classes.Authentication.LdapAuthentication.IsAuthenticated()
   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_AdsObject()
   at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
   at System.DirectoryServices.DirectorySearcher.FindOne()
   at Chain_Of_Custody.Classes.Authentication.LdapAuthentication.IsAuthenticated()

認証タイプを変更しようとしましたが、何が起こっているのかわかりません。


こちらも参照: ユーザー名とパスワードを Active Directory に対して検証しますか?

役に立ちましたか?

解決

.net 3.5を使用している場合は、代わりにこのコードを使用します。

ユーザーを認証するには:

PrincipalContext adContext = new PrincipalContext(ContextType.Domain);

using (adContext)
{
     return adContext.ValidateCredentials(UserName, Password);
}

オブジェクトのR / W属性に対するユーザーを見つける必要がある場合は、次のようにします。

PrincipalContext context = new PrincipalContext(ContextType.Domain);
UserPrincipal foundUser = 
    UserPrincipal.FindByIdentity(context, "jdoe");

これはSystem.DirectoryServices.AccountManagement名前空間を使用しているため、usingステートメントに追加する必要があります。

レガシーコードを使用するためにUserPrincipalオブジェクトをDirectoryEntryオブジェクトに変換する必要がある場合は、次の操作を実行できます。

DirectoryEntry userDE = (DirectoryEntry)foundUser.GetUnderlyingObject();

他のヒント

同じコードが複数のWebサイトでインターネット上に浮かんでいることがわかりましたが、うまくいきませんでした。 Steve Evansは、おそらく.NET 3.5を使用している場合は、このコードを使用しないでください。ただし、まだ.NET 2.0を使用している場合は、これを試してADサービスの認証を行うことができます。

DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, 
   userName, password, 
   AuthenticationTypes.Secure | AuthenticationTypes.SecureSocketsLayer);
object nativeObject = entry.NativeObject;

最初の行は、ドメイン、ユーザー名、およびパスワードを使用してDirectoryEntryオブジェクトを作成します。また、AuthenticationTypesも設定します。 「Bitwise OR」を使用してセキュア(Kerberos)認証とSSLの両方を設定していることに注目してください。 ( '|')2つのパラメーター間の演算子。

2行目は、NativeObjectに「エントリ」を強制します。最初の行の情報を使用してADサービスにバインドします。

例外がスローされた場合、資格情報(または設定)が不良でした。例外がなければ、認証されています。例外メッセージは通常、何が問題なのかを示します。

このコードは既にあるものとかなり似ていますが、「パス」がある場所でドメインが使用され、ユーザー名はドメインと結合されていません。 AuthenticationTypesも適切に設定してください。これにより、認証機能が有効または無効になります。

とにかくそれを理解しました。vistaでユーザー名を持つドメインを渡すと、「ドメイン\ユーザー」のように機能しません。したがって、「ユーザー」を渡すだけです。代わりに問題なく動作しているようです-同じドメインにいる必要がある場合を除いて

LDAP へのバインドには昇格された特権 (UAC) が必要ですか?Visual Studio やアプリを管理者として実行して、問題が解決するかどうかを確認してください。それが問題である場合は、いつでもアプリケーションにマニフェストを追加し、昇格を必要とするように設定できます。そうすれば、ユーザーがアプリケーションを実行するときにプロンプ​​トが表示されます。

なぜ特権昇格が必要になるのかはわかりませんが、試してみる価値はあります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top