0

UPDATE: I found out that it is not an issue with azure rather the other party who are actually doing white-listing on incoming connection.

I implemented a service, which open and ftp connection to upload a file to a remote FTP server. The code works perfectly (!) running on my desktop, however if I publish it to the azure hosting cloud and execute with exactly the same parameters I get WebException with the message 'The remote server returned an error: (530) Not logged in.' I haven't found any relevant setting in the azure console. I also tried different setting like on/off keep-alive and passive, but the result are the same. Do you guys have an idea what could be the problem?

The code for reference:

            Dim request As FtpWebRequest

            request = TryCast(WebRequest.Create($"ftp://{_host}/inventory/{_uploadFilename}"), FtpWebRequest)
            request.Method = WebRequestMethods.Ftp.UploadFile
            request.Credentials = New NetworkCredential(_username, _password)
            request.UsePassive = True
            request.UseBinary = True
            request.KeepAlive = False

            Using fs As FileStream = File.OpenRead(_fileName)
                Dim buffer As Byte() = New Byte(fs.Length - 1) {}
                fs.Read(buffer, 0, buffer.Length)
                fs.Close()
                Dim requestStream As Stream = request.GetRequestStream()
                requestStream.Write(buffer, 0, buffer.Length)
                requestStream.Flush()
                requestStream.Close()


                Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
                Return Ok($"Remote server response: {response.StatusDescription}")

            End Using
kexx
  • 275
  • 5
  • 13

2 Answers2

1

Where is NetworkCredential getting credentials from?

Windows Authentication (Kerberos/NTLM) won't work in Azure. You need to use FTP login (as in USER and PASS FTP commands).

Always use PASSIVE mode when attempting FTP transfer from Azure.

Imports System.IO
Imports System.Net

Module Module1

    Sub Main()
        Dim request As FtpWebRequest
        Dim _host = "remote.ftpserver.com"
        Dim _uploadFilename = "file.txt"
        Dim _filename = "file.txt"
        Dim _username = "Username"
        Dim _password = "PaSsw0rD"

        request = TryCast(WebRequest.Create($"ftp://{_host}/path/{_uploadFilename}"),
            FtpWebRequest)
        request.Method = WebRequestMethods.Ftp.UploadFile
        request.Credentials = New NetworkCredential(_username, _password)
        request.UsePassive = True
        request.UseBinary = True
        request.KeepAlive = False

        Using fs As FileStream = File.OpenRead(_filename)
            Dim buffer As Byte() = New Byte(fs.Length - 1) {}
            fs.Read(buffer, 0, buffer.Length)
            fs.Close()
            Dim requestStream As Stream = request.GetRequestStream()
            requestStream.Write(buffer, 0, buffer.Length)
            requestStream.Flush()
            requestStream.Close()
            Dim response As FtpWebResponse = DirectCast(request.GetResponse(),
                FtpWebResponse)
            Console.WriteLine($"Remote server response: {response.StatusCode}, 
                {response.StatusDescription}")
        End Using
    End Sub

End Module

Apologies if the formatting is all over the place. This is my first attempt at writing VB.

Running this in Kudu's DebugConsole for clarity - same sandbox as the Web App:

Run-in-Kudu

On the remote FTP server:

ftp> ls file.txt
200 PORT command successful.
125 Data connection already open; Transfer starting.
file.txt
226 Transfer complete.
ftp: 13 bytes received in 0.02Seconds 0.81Kbytes/sec.

ftp> bin
200 Type set to I.

ftp> get file.txt
200 PORT command successful.
125 Data connection already open; Transfer starting.
226 Transfer complete.
ftp: 24 bytes received in 0.18Seconds 0.14Kbytes/sec.

ftp> !type file.txt
Upped with FtpWebRequest
evilSnobu
  • 24,582
  • 8
  • 41
  • 71
  • Thank you for take a look at my problem, however I think it is not about being a kerberos authentication. It is just a simple basic one. I run the code on a local ftp server, and here is the relevant commands executed: 2016-09-28 19:16:48 ::1 - ::1 21 USER anonymous 331 0 0 72cfd8e0-20c5-48ad-af21-df2443de8605 - 2016-09-28 19:16:48 ::1 - ::1 21 PASS devtest@helloka.com 230 0 0 72cfd8e0-20c5-48ad-af21-df2443de8605 / However I'm kinda interested how did you do testing. Did you somehow upload a vb console application to azure? – kexx Sep 28 '16 at 19:21
  • Yes. Drag and drop into the Kudu console. Link in my answer. – evilSnobu Sep 28 '16 at 20:37
1

I found out that it is not an issue with azure rather the other party who are actually doing white-listing on incoming connection.

kexx
  • 275
  • 5
  • 13