none
FtpWebRequest Errror. The remote server returned an error: (530) Not logged in RRS feed

  • Question

  • I have the following code and its giving an error "The remote server returned an error: (530) Not logged in". The Response part of the System.Net.WebException is saying "530 No client certificate presented.".  So, its something related to the certificate that i am doing wrong. The code is shown as follows.

                string ServerAddress = "someserver.xyz.com";
                string SubFolder = "toSSC";
                string fileName = "mydatafile.csv";
                string UserName = "testusername";
                string Password = "Password#";
                string CertificatePath = @"C:\Temp\Cert.pfx";
                string CertificatePassKey = "Passkey#";
    
                string ftpURL = string.Format(@"ftp://{0}//{1}", ServerAddress, SubFolder);
     
                FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(ftpURL);
                ftpRequest.Credentials = new NetworkCredential(UserName.Normalize(), Password.Normalize());
                ftpRequest.UsePassive = true;
    
                X509Certificate certificate = new X509Certificate(CertificatePath, CertificatePassKey.Normalize());
    
                //Create a certificate collection object and add created certificate to it.
                X509CertificateCollection certCollection = new X509CertificateCollection();
                certCollection.Add(certificate);
    
                //set SSL setting for FTPWebRequest object
                ftpRequest.EnableSsl = true;
                ftpRequest.ClientCertificates = certCollection;
    
                using (FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse())
                {
                }


    There is a 3rd party tool called WINSCP which is allowing us to connect using the following code. I understand that its a high level library and we dont know whats going on underneath but its frustrating to know that this 3rd party library is working and i am probably making some mistake in my above .NET code.

                SessionOptions sessionOptions = new SessionOptions
                {
                    Protocol = Protocol.Ftp,
                    HostName = "someserver.xyz.com",
                    UserName = "testusername",
                    Password = "Password#",
                    FtpSecure = FtpSecure.Explicit,
                    TlsClientCertificatePath = @"C:\Temp\Cert.pfx",
                    PrivateKeyPassphrase = "Passkey#"
                };
    
                using (Session session = new Session())
                {
                    // Connect
                    session.Open(sessionOptions);
    
                    // Your code
                }

    Would really appreciate some help.

    Wednesday, November 1, 2017 9:39 PM

All replies

  • Hi TarunPnkl,

    Thank you for posting here.

    For your question, I create a FTP to access. When you use the FtpWebRequest to connect to ftp with Credentials, it is enough to use NetworkCredential(UserName, PassWord).

     // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://localhost/");
               
                request.Method = WebRequestMethods.Ftp.ListDirectory;
    
               
                request.Credentials = new NetworkCredential("UserName", "PassWord");
    
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                Console.WriteLine(reader.ReadToEnd());
    
                Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);
    
                reader.Close();
                response.Close();

    It works well for me. Please check the code. If you still have login problem, please check the permission of you application and the UserName and PassWord.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, November 3, 2017 7:35 AM
    Moderator
  • Hi TarunPnkl,

    Have you solved your problem?

    If you solved your problem, please mark the useful reply as answer. This will make answer searching in the forum and be beneficial to community members as well.

    If you have something else about this issue, please feel free to contact us.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 16, 2017 2:48 PM
    Moderator