Delphi/Rdp check username and password before conn

2019-07-08 02:52发布

问题:

as the subject say am trying to connect to server using this code in delphi

procedure TmainF.Button1Click(Sender: TObject);
var 
   rdp1 : TMsRdpClient7NotSafeForScripting ;
begin
  rdp1 := TMsRdpClient7NotSafeForScripting.Create(self);
  rdp1.Parent := mainF;
  rdp1.Server:=server_name;
  rdp1.UserName := user.Text;
  rdp1.AdvancedSettings7.ClearTextPassword := password.Text;
  rdp1.ConnectingText := 'connecting';
  rdp1.DisconnectedText := 'disconnected';
  rdp1.AdvancedSettings7.AuthenticationLevel:=0;
  rdp1.AdvancedSettings7.EnableCredSspSupport:=true;
  rdp1.Connect;
end;

the code is working fine but if the user entered a wrong user name or password
the rdp object is showing the remote desktop login prompt box to reenter the user name or password like the image

I want to validate the username and password before connect
or prevent this box and show a custom message from my app

I tried to use OnLogonError() procedure but it's not fired any code
and I read this Question but the code is c# and I confused with .getocx() and I can't find (PromptForCredentials) in (MSTSCLib_TLB.pas)

any help :( ??

sorry for my bad English.

回答1:

There is an interface IMsRdpClientNonScriptable5 that has the following methods:

  • Get_AllowPromptingForCredentials()
  • Set_AllowPromptingForCredentials()

If you call Set_AllowPromptingForCredentials() with the parameter set to false the dialog won't appear.

As for how to get an instance of this interface - easy, you just cast the ControlInterface of your RDP client object:

Ircns5 := rdp1.ControlInterface as IMsRdpClientNonScriptable5;
if Assigned(Ircns5) then
  Ircns5.Set_AllowPromptingForCredentials(False);


回答2:

You could pass the username and password to the Windows API function "LogonUser" before establishing the connection. For this to work the authenticating entity must be available on the user's network.

{$APPTYPE CONSOLE}

uses SysUtils, Windows;

var
    hToken : THandle;

begin
    if (ParamCount <> 3) then
    begin
        WriteLn ('LogonUserTest.exe [user name] [domain] [password]');
        Halt ($FF);
    end; { if }

    try
        Win32Check (LogonUser (PChar (ParamStr (1)), PChar (ParamStr (2)),
                               PChar (ParamStr (3)), Logon32_Logon_Interactive,
                               LOGON32_PROVIDER_DEFAULT, hToken));
        WriteLn ('LogonUser success');
        CloseHandle (hToken);

    except
        on E: Exception do
            Writeln (E.ClassName, ': ', E.Message);
    end; { try / except }
end.