2

Active Directory API を使用しており、次のコードを使用してサーバーに接続しようとしています。

PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, (server + ":" + port), loginUsername, loginPassword);

無効なログイン ユーザー名またはパスワードが渡されるたびに、ステートメント全体からの例外はスローされませんが、次のコードは実行を続けます。PrincipalContextデバッグ時に、以下に示すように、クラスがエラーをスローすることがわかりました。

ここに画像の説明を入力

これらは、クラス内に含まれる 2 つのプロパティです。「ConnectedServer」プロパティをさらに調べると、デバッガに次のように表示されます。

ここに画像の説明を入力

私の問題は、エラーが外部にスローされないため、このエラーを実際に確認する方法がわからないことです。ユーザー名またはパスワードが無効な場合に簡単なエラー メッセージを表示したいと思います。基本的に、上記のエラーがスローされたかどうかを確認する方法を見つけます。

これはどのように行うことができますか?

4

4 に答える 4

1

プリンシパル コンテキストからの例外を処理する最善の方法は、コードを try に配置し、次に示すように例外をキャッチすることです。

        string user = txtUsername.Text;
        string pass = txtPassword.Text;

        //start a try and catch method
      try
      {

       //create a principalcontext object

        var pc = new PrincipalContext(ContextType.Domain, "*****", user, pass);
         {


               //validate the user credentials
                if (pc.ValidateCredentials(user, pass))
                {
                    //create a user identity
                    UserPrincipal userp = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, user);

                    //check if the user is returned

                    if (userp != null)
                    {
                        //if user exists, return an array of authorized groups
                        var grps = userp.GetAuthorizationGroups();

                        //convert the array to a list to enable search of CIS group
                        List<string> strList = grps.Select(o => o == null ? String.Empty : o.ToString()).ToList();

                        //check for CIS group from the list
                        if (strList.Contains("CISS"))
                        {
                            //create a session variable to show the loggedin user and set the error panel to false
                            Session["username"] = user;
                            ErrorPanel.Visible = false;

                            //redirect the user to the homepage
                            Response.Redirect("appdesk/account.aspx");
                        }
                        else if (!strList.Contains("CISS"))
                        {
                            Label1.Text = "You Don't have the Rights to login to the platfrom";
                            ErrorPanel.Visible = true;

                    }
                }
                 //if the user credentials are invalid
                if (!pc.ValidateCredentials(user, pass))
                {
                    Label1.Text = "Login Failed.Incorrect Username or Password";
                    ErrorPanel.Visible = true;


                }
             }
        }
          //catch the exceptions in the try
       catch (Exception exc)
       {
                Label1.Text = exc.Message.ToString();
                ErrorPanel.Visible = true;                    

       }
于 2016-09-30T13:34:59.563 に答える
1

のクラスSystem.DirectoryServices.AccountManagementは異なる実行です。必要になるまで、Active Directory サーバーへの接続を試みません。ValidateCredentialsメソッドは、MSDN からのチェックを強制する方法です。

ValidateCredentials メソッドは、コンストラクターで指定されたサーバーにバインドします。ユーザー名とパスワードのパラメーターが null の場合、コンストラクターで指定された資格情報が検証されます。コンストラクターで資格情報が指定されておらず、ユーザー名とパスワードのパラメーターが null の場合、このメソッドは現在のプリンシパルの既定の資格情報を検証します。

だからあなたがする必要があるのは

using(PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, (server + ":" + port), loginUsername, loginPassword))
{
    //This will force the connection to the server and validate that the credentials are good
    //If the connection is good but the credentals are bad it will return "false", if the connection is bad it will throw a exception of some form.
    if(principalContext.ValidateCredentials(null, null))
    {
        // Rest of code here.

        //This is how you do the same check you where doing in your previous quesiton, notice that this is "userName", and "password" not "loginUsername" and "loginPassword"
        valid = principalContext.ValidateCredentials(userName,password);

    }
}
于 2013-11-11T15:19:47.510 に答える
0

基本的なキャッチがうまくいかない?何かのようなもの:

private ADConnectResults Connect(string server, int port)
try
{
    PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, (server + ":" + port), loginUsername, loginPassword);
    return new ADConnectResults(true, principalContext);
}
catch(DirectoryServicesCOMException dex)
{
     Log(dex);
     return new ADConnectResults(false);
}
}
于 2013-11-11T15:21:02.880 に答える