1

I have 1 issues when i try too send file using FTP from Azure WebJobs. This throw alternatly 'The remote server returned an error: (530) Not logged in.', my code work great in localhost (on dev computer).

I have read all off thoses Post but i didn't find a way :

FTPWebRequest 530 Error: Not Logged in issue

FTP The remote server returned an error: (530) Not logged in

Fetching files from FTP server via Azure webjobs

Ftp to external server in azure webjob not working

Other but i have only 1 webjobs in my app service and the CPU was at 30% when the jobs running .. File download from FTP fails only in Azure web app

Edit : Code Make Directory

FtpWebRequest reqFTP = null;
        Stream ftpStream = null;

        string[] subDirs = directory.Split('/');

        string currentDir = publishUrl;

        foreach (string subDir in subDirs)
        {
            try
            {
                currentDir = currentDir + "/" + subDir;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(currentDir);
                reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
                reqFTP.UseBinary = true;
                //reqFTP.UsePassive = true;
                //reqFTP.KeepAlive = true;
                reqFTP.Credentials = new NetworkCredential(userName, userPWD);
                FtpWebResponse response =  (FtpWebResponse)await reqFTP.GetResponseAsync();
                ftpStream = response.GetResponseStream();
                ftpStream.Close();
                response.Close();
            }
            catch (Exception exception)
            {

                Console.WriteLine(exception.Message);
                //directory already exist I know that is weak but there is no way to check if a folder exist on ftp...
            }
        }

Code Send File :

        try
        {
            using (WebClient client = new WebClient())
            {
                client.Credentials = new NetworkCredential(userName, userPWD);
                client.UploadFile(publishUrl, "STOR", localFileName);
                return true;
            }
        }
        catch (Exception exception)
        {
            //Console.Error.Write(exception.Message);
            Console.WriteLine(exception.Message);
            return false;
        }

And log when i run code in Azure WebApp WebJobs (Failed) : https://pastebin.com/vgTxqT5p

And log when i run code in local machine (Work Great) : https://pastebin.com/hBpum8T0

I think the workaround was on WebJobs app style and normal function wasn't waiting. I'm going to change my code to use Async Await method for all my WebJobs program.

Any have a way ? Thx in advance.

Sanpas
  • 1,170
  • 10
  • 29
  • Show us your code and [log file](https://stackoverflow.com/q/9664650/850848). – Martin Prikryl Oct 18 '18 at 14:25
  • Thx I have put my code, i'm going to generate log file. I have open a ticket on Azure Support too. – Sanpas Oct 18 '18 at 14:29
  • 1
    To implemented log file on WebJobs Azure : https://blogs.msdn.microsoft.com/waws/2017/03/14/how-to-get-a-system-net-trace-of-your-c-web-job/ – Sanpas Oct 18 '18 at 14:45
  • @MartinPrikryl log file was added. https://pastebin.com/vgTxqT5p – Sanpas Oct 18 '18 at 15:26
  • There are far too many requests in each logs so its difficult to follow. But obviously some requests succeed to login, while some not. It looks like you are hitting some limit on the server regarding number of opened connections. You should consult the server administrator. – Martin Prikryl Oct 18 '18 at 16:25
  • What definitely does not help is the load of failed directory creation attempts (as the directories already exists). Each failed requests causes a new connection. – Martin Prikryl Oct 18 '18 at 16:30
  • The server may have IP-based rules. No one but server administrator can tell you why the server is rejecting authentication. So far this does not look like a programming question. – Martin Prikryl Oct 18 '18 at 16:41
  • I didn't say Azure Web Application secret :D .. Thx for your help @MartinPrikryl. I'have open ticket on Azure Portal Support I'm awaiting they call.. I didn't know Azure Web App limitation. – Sanpas Oct 18 '18 at 16:46
  • Ok soo i'm looking to find if the exception it's not due too the level off my plan services in production env (I'm gooing to deploy app on production on other level plan sevices) – Sanpas Oct 19 '18 at 07:16
  • What "plan"? Azure plan? – Martin Prikryl Oct 19 '18 at 07:34
  • Yes Azure Plan Services (it's like a server ressources) – Sanpas Oct 19 '18 at 07:46
  • I already wrote that it's the FTP server that rejects the connection. No change on client (Azure) side can fix this. You can see in the log files that your application sends the very same login request to the FTP server, no matter if you run it locally or on Azure. So the **problem is on the FTP server**. Again, talk to the FTP server administrator. – Martin Prikryl Oct 19 '18 at 08:10
  • Yes i see this but the FTP destination it's not a server part it's too a WebApplication Azure on the same Azure Plan Services – Sanpas Oct 19 '18 at 10:10
  • If it's your FTP server, then check its log file. – Martin Prikryl Oct 19 '18 at 10:37

1 Answers1

0

Bad way (and i didn't Like this) but it's work..... :

Change the Make Directory Function like this :

public static void MakeFTPDir(string publishUrl, string userName, string userPWD, string directory)
    {
        FtpWebRequest reqFTP = null;
        Stream ftpStream = null;

        string[] subDirs = directory.Split('/');

        string currentDir = publishUrl;

        foreach (string subDir in subDirs)
        {
            bool isNotCreated = true;
            int iTentative = 0;
            currentDir = currentDir + "/" + subDir;
            while (isNotCreated)
            {
                iTentative++;
                try
                {
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(currentDir);
                    reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
                    reqFTP.UseBinary = true;
                    reqFTP.UsePassive = true;
                    reqFTP.KeepAlive = true;
                    reqFTP.Credentials = new NetworkCredential(userName, userPWD);
                    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                    ftpStream = response.GetResponseStream();
                    ftpStream.Close();
                    response.Close();
                }
                catch(WebException webException)
                {
                    FtpWebResponse excWebResponse = (FtpWebResponse)webException.Response;
                    if(excWebResponse.StatusCode == FtpStatusCode.NotLoggedIn)
                    {
                        Console.WriteLine("WebException ==> NotLoggedIn >> Tentative :" + iTentative);
                        isNotCreated = true;
                    }
                    else
                    {
                        Console.WriteLine(webException.Message);
                        isNotCreated = false;
                    }
                }
                catch (Exception exception)
                {
                    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                    if (response.StatusCode == FtpStatusCode.NotLoggedIn)
                    {
                        Console.WriteLine("Exception ==> NotLoggedIn >> Tentative :" + iTentative);
                        isNotCreated = true;
                    }
                    else
                    {
                        Console.WriteLine(exception.Message);
                        isNotCreated = false;
                    }
                }
            }
        }
    }

Change the send file function like this

 public static bool SendFtpFile(string publishUrl, string userName, string userPWD, string localFileName)
    {
        bool isNotCreated = true;
        int iTentative = 0;
        while (isNotCreated)
        {
            iTentative++;
            try
            {
                using (WebClient client = new WebClient())
                {
                    client.Credentials = new NetworkCredential(userName, userPWD);
                    client.UploadFile(publishUrl, "STOR", localFileName);
                    return true;
                }
            }
            catch (WebException webException)
            {
                FtpWebResponse excWebResponse = (FtpWebResponse)webException.Response;
                if (excWebResponse.StatusCode == FtpStatusCode.NotLoggedIn)
                {
                    Console.WriteLine("WebException ==> NotLoggedIn >> Tentative :" + iTentative);
                    isNotCreated = true;
                }
                else
                {
                    Console.WriteLine(webException.Message);
                    return false;
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
                return false;
            }
        }
        return true;
    }

Change the IfFileExistOnServer :

public static bool CheckIfFileExistsOnServer(string publishUrl, string userName, string userPWD, string fileName)
    {
        bool isNoCheck = true;
        int iTentative = 0;
        string azureBotUrl = publishUrl + "/" + fileName;

        while (isNoCheck)
        {
            iTentative++;
            try
            {
                var request = (FtpWebRequest)WebRequest.Create(azureBotUrl);
                request.Credentials = new NetworkCredential(userName, userPWD);
                request.UseBinary = true;
                request.UsePassive = true;
                request.KeepAlive = true;
                request.Method = WebRequestMethods.Ftp.GetFileSize;
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                return true;
            }
            catch (WebException webException)
            {
                FtpWebResponse excWebResponse = (FtpWebResponse)webException.Response;
                if (excWebResponse.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                    return false;
                if (excWebResponse.StatusCode == FtpStatusCode.NotLoggedIn)
                {
                    Console.WriteLine("WebException ==> NotLoggedIn >> Tentative :" + iTentative);
                    isNoCheck = true;
                }
                else
                {
                    return false;
                }
            }
        }
        return false;
    }

And Change RenameFileOnServer :

public static bool RenameFileOnServer(string publishUrl, string userName, string userPWD, string sourceFileName, string newFileName)
    {
        bool isNoRenameFile = true;
        int iTentative = 0;
        FtpWebRequest ftpRequest = null;
        FtpWebResponse ftpResponse = null;
        string azureBotUrl = publishUrl + "/" + sourceFileName;
        while (isNoRenameFile)
        {
            iTentative++;
            try
            {
                ftpRequest = (FtpWebRequest)WebRequest.Create(azureBotUrl);
                ftpRequest.Credentials = new NetworkCredential(userName, userPWD);
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                ftpRequest.Method = WebRequestMethods.Ftp.Rename;
                ftpRequest.RenameTo = newFileName.Split('\\')[newFileName.Split('\\').Length - 1];
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                ftpResponse.Close();
                ftpRequest = null;
                return true;
            }
            catch (WebException webException)
            {
                FtpWebResponse excWebResponse = (FtpWebResponse)webException.Response;
                if (excWebResponse.StatusCode == FtpStatusCode.NotLoggedIn)
                {
                    Console.WriteLine("WebException ==> NotLoggedIn >> Tentative :" + iTentative);
                    isNoRenameFile = true;
                }
                else
                {
                    return false;
                }
                Console.WriteLine(webException.Message);
            }
            catch (Exception)
            {
                return false;
            }
        }
        return false;
    }

I'm waiting a call from Ms Azure Support ...

Sanpas
  • 1,170
  • 10
  • 29
  • I'm experiencing the same issue, but with a PowerShell script, is there any update from Ms Azure Support? – Avi Siboni Jun 08 '21 at 14:38
  • @avisiboni i'm sorry but doesn't work in any more on this project. I think have some API or best practices todo this Have you see this : https://winscp.net/eng/docs/guide_microsoft_azure_webjob_sftp – Sanpas Jun 09 '21 at 15:24