PowerTCP FTP for .NET
Authenticate(String,String) Method
Example 



The username to use for login.
The password to use for login.
Logs into the FTP server using username, password, and other configuration settings specified in Session.
Syntax
Public Overloads Function Authenticate( _
   ByVal username As String, _
   ByVal password As String _
) As List(Of Response)
Dim instance As Ftp
Dim username As String
Dim password As String
Dim value As List(Of Response)
 
value = instance.Authenticate(username, password)
public List<Response> Authenticate( 
   string username,
   string password
)
public:
List<Response^>^ Authenticate( 
   String^ username,
   String^ password
) 

Parameters

username
The username to use for login.
password
The password to use for login.

Return Value

A List of Response objects generated.
Remarks

Sets Session.Username and Session.Password to the provided values and calls Authenticate.

This method or Authenticate should be used after Connect or Connect(String) (or another login technique must be used).

SSL encryption is negotiated before logging in if Session.Security.EncryptionControl == EncryptionControl.Explicit.

Example
The following example demonstrates connecting to an Ftp server, with options for encryption (FTPS: Explicit/Implicit), and authenticating the user.
using System.Security.Cryptography.X509Certificates;
using System.Security.Authentication;
using System.Net.Security;

/// <summary>
/// Connects to an Ftp server, optionally using encryption (Explicit/Implicit), and authenticates the user.
/// </summary>
/// <param name="myFtp">The Ftp instance to connect and authenticate</param>
/// <param name="hostNameOrAddress">The server's hostname or IP address</param>
/// <param name="port">Port that the server is listening on. Usually 21 for Explicit or non-secure, 990 for Implicit.</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <param name="encryptControl">Controls whether SSL/TLS is used, and the implementation. None/Explicit/Implicit.</param>
/// <param name="encryptData">Controls whether the data channel is encrypted.</param>
private void ConnectFtp(Ftp myFtp, string hostNameOrAddress, int port, string username, string password, EncryptControl encryptControl, bool encryptData)
{
    //Set the server address
    myFtp.Session.RemoteEndPoint = new IPEndPoint(hostNameOrAddress, port);

    if (encryptControl != EncryptControl.None)
    {
        //Set the control channel's security protocol - Implicit/Explicit
        myFtp.Session.Security.EncryptControl = encryptControl;

        //Set whether the data channel should be encrypted. This may or may not be required by your FTP server.
        myFtp.Session.Security.EncryptData = encryptData;

        //Optionally set the protocols available for SSL/TLS negotiation (defaults to SslProtocols.Default)
        //TLS 1.1/1.2 requires .NET 4.5+. See the SslProtocols MSDN documentation for more information.
        myFtp.Session.Security.Protocols = SslProtocols.Tls | SslProtocols.Ssl3;

        //Specify the server certificate validation callback
        myFtp.Session.Security.ValidationCallback = remoteCertificateValidation;
    }

    //Connect to the server.
    myFtp.Connect();

    //Authenticate the user.
    myFtp.Authenticate(username, password);
}

private bool remoteCertificateValidation(Object sender, X509Certificate remoteCertificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    //For this simple snippet, accept all server certificates. Please see the 'Security' top-level help topics for more information, or 
    //the System.Net.Security.RemoteCertificateValidationCallback MSDN documentation.
    return true;
}
Imports System.Security.Cryptography.X509Certificates
Imports System.Security.Authentication
Imports System.Net.Security

''' <summary>
''' Connects to an Ftp server, optionally using encryption (Explicit/Implicit), and authenticates the user.
''' </summary>
''' <param name="myFtp">The Ftp instance to connect and authenticate</param>
''' <param name="hostNameOrAddress">The server's hostname or IP address</param>
''' <param name="port">Port that the server is listening on. Usually 21 for Explicit or non-secure, 990 for Implicit.</param>
''' <param name="username">Username</param>
''' <param name="password">Password</param>
''' <param name="encryptControl">Controls whether SSL/TLS is used, and the implementation. None/Explicit/Implicit.</param>
''' <param name="encryptData">Controls whether the data channel is encrypted.</param>
Private Sub ConnectFtp(ByVal myFtp As Ftp, ByVal hostNameOrAddress As String, ByVal port As Integer, ByVal username As String, ByVal password As String, ByVal encryptControl As EncryptControl, ByVal encryptData As Boolean)
    'Set the server address
    myFtp.Session.RemoteEndPoint = New IPEndPoint(hostNameOrAddress, port)

    If encryptControl <> Dart.Ftp.EncryptControl.None Then
        'Set the control channel's security protocol - Implicit/Explicit
        myFtp.Session.Security.EncryptControl = encryptControl

        'Set whether the data channel should be encrypted. This may or may not be required by your FTP server.
        myFtp.Session.Security.EncryptData = encryptData

        'Optionally set the protocols available for SSL/TLS negotiation (defaults to SslProtocols.Default)
        'TLS 1.1/1.2 requires .NET 4.5+. See the SslProtocols MSDN documentation for more information.
        myFtp.Session.Security.Protocols = SslProtocols.Tls Or SslProtocols.Ssl3

        'Specify the server certificate validation callback
        myFtp.Session.Security.ValidationCallback = AddressOf remoteCertificateValidation
    End If

    'Connect to the server.
    myFtp.Connect()

    'Authenticate the user.
    myFtp.Authenticate(username, password)
End Sub

Private Function remoteCertificateValidation(ByVal sender As Object, ByVal remoteCertificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) As Boolean
    'For this simple snippet, accept all server certificates. Please see the 'Security' top-level help topics for more information, or 
    'the System.Net.Security.RemoteCertificateValidationCallback MSDN documentation.
    Return True
End Function
See Also

Reference

Ftp Class
Ftp Members
Overload List


PowerTCP FTP for .NET Documentation Version 6.1
© 2023 Dart Communications. All Rights Reserved.
Send comments on this topic