Exemplo n.º 1
0
        /// <summary>
        ///     Attempts to log the user in to the FTP Server
        /// </summary>
        /// <returns></returns>
        public async Task LoginAsync()
        {
            if (IsConnected)
            {
                await LogOutAsync();
            }

            string username = Configuration.Username.IsNullOrWhiteSpace()
                ? Constants.ANONYMOUS_USER
                : Configuration.Username;

            await ControlStream.ConnectAsync();

            var usrResponse = await ControlStream.SendCommandAsync(new FtpCommandEnvelope
            {
                FtpCommand = FtpCommand.USER,
                Data       = username
            });

            await BailIfResponseNotAsync(usrResponse, FtpStatusCode.SendUserCommand, FtpStatusCode.SendPasswordCommand, FtpStatusCode.LoggedInProceed);

            var passResponse = await ControlStream.SendCommandAsync(new FtpCommandEnvelope
            {
                FtpCommand = FtpCommand.PASS,
                Data       = username != Constants.ANONYMOUS_USER ? Configuration.Password : string.Empty
            });

            await BailIfResponseNotAsync(passResponse, FtpStatusCode.LoggedInProceed);

            IsAuthenticated = true;

            if (ControlStream.IsEncrypted)
            {
                await ControlStream.SendCommandAsync(new FtpCommandEnvelope
                {
                    FtpCommand = FtpCommand.PBSZ,
                    Data       = "0"
                });

                await ControlStream.SendCommandAsync(new FtpCommandEnvelope
                {
                    FtpCommand = FtpCommand.PROT,
                    Data       = "P"
                });
            }

            Features = await DetermineFeaturesAsync();

            directoryProvider = DetermineDirectoryProvider();
            await EnableUTF8IfPossible();
            await SetTransferMode(Configuration.Mode, Configuration.ModeSecondType);

            if (Configuration.BaseDirectory != "/")
            {
                await CreateDirectoryAsync(Configuration.BaseDirectory);
            }

            await ChangeWorkingDirectoryAsync(Configuration.BaseDirectory);
        }