PowerTCP SSH and SFTP for .NET
Challenge Event
Example 




Raised when the server presents its challenges during 'keyboard-interactive' authentication.
Syntax
'Declaration
 
Public Event Challenge As SshConnection.ChallengeEventHandler
'Usage
 
Dim instance As SshConnection
Dim handler As SshConnection.ChallengeEventHandler
 
AddHandler instance.Challenge, handler
public event SshConnection.ChallengeEventHandler Challenge
public: __event SshConnection.ChallengeEventHandler* Challenge
public:
event SshConnection.ChallengeEventHandler^ Challenge
Event Data

The event handler receives an argument of type ChallengeEventArgs containing data related to this event. The following ChallengeEventArgs properties provide information specific to this event.

PropertyDescription
Echo Gets information sent by the server that indicates whether responses will be echoed back to the client.  
Instruction Gets the instruction string sent by the server.  
Name Gets the name string sent by the server.  
Prompt Gets the prompt or prompts sent by the server.  
Response Gets a string array that the user should initialize to values that will be sent to the server for authentication. Each Response correlates to a Prompt at the same index.  
Remarks

For each ChallengeEventArgs.Prompt, populate a ChallengeEventArgs.Response at the same index to proceed with authentication.

See the ComponentBase.SynchronizingObject property for important information on updating UI controls from within this event.

Example
The example below demonstrates 'keyboard-interactive' authentication with the Challenge event.
private void button1_Click(object sender, System.EventArgs e)
{
    //Connect to the server
    ssh1.Connection.RemoteEndPoint.HostNameOrAddress = myServerHostname;
    ssh1.Connection.RemoteEndPoint.Port = myServerPort; //Usually 22
    ssh1.Connect();

    ssh1.Authenticate(myUsername, new string[] { });
}

private void ssh1_Challenge(object sender, ChallengeEventArgs e)
{
    for (int i = 0; i <= e.Prompt.Length - 1; i++)
    {
        if (e.Prompt[i] == "password:")
            e.Response[i] = myPassword;
        //If the server presents additional challenges, populate each response here
    }
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
    'Connect to the server
    ssh1.Connection.RemoteEndPoint.HostNameOrAddress = myServerHostname
    ssh1.Connection.RemoteEndPoint.Port = myServerPort 'Usually 22
    ssh1.Connect()

    ssh1.Authenticate(myUsername, New String() { })
End Sub

Private Sub ssh1_Challenge(ByVal sender As Object, ByVal e As ChallengeEventArgs) Handles ssh1.Challenge
    For i As Integer = 0 To e.Prompt.Length - 1
        If e.Prompt(i) = "password:" Then
            e.Response(i) = myPassword
        End If
        'If the server presents additional challenges, populate each response here
    Next i
End Sub
See Also

Reference

SshConnection Class
SshConnection Members


PowerTCP SSH and SFTP for .NET Documentation Version 7.0
© 2023 Dart Communications. All Rights Reserved.
Send comments on this topic