Recently working on a FTP solution using C# , i encountered an error The remote server returned an error: (530) Not logged in.”

The code i used was following

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp://xxxxxx/file.txt); 
request.Method = WebRequestMethods.Ftp.UploadFile
request.Credentials = new NetworkCredential(usernameVariable, passwordVariable); 

What was more bewildering was if i modified the code to following, the solution was working fine. But this for obvious reasons is not an option as the username cannot be hardcoded

//works but implausible to use in realtime solutions
request.Credentials = new NetworkCredential("dmn/#gsgs", password);  

Some googling revealed that special charcters create issues in the NetworkCredential Object. Hence some playing around worked for me, and it works irrespective of wether i do a FTPWebRequest or WebRequest.

Solution:

Instantiate NetworkCredential object with three paramters (username, password, domain) and make sure to normalize the string variables while instantiating the NetworkCredential Object.

i.e:

//this works
request.Credentials = new NetworkCredential(usernameVariable.Normalize(),passwordVariable.Normalize(),domainVariable.Normalize()); 

Note: Normalize the string while instantiating the NetworkCredential object like above . The following doesnt work:

string usr = usernameVariable.Normalize()
string pwd= passwordVariable.Normalize()
string dom = domainVariable.Normalize()
request.Credentials = new NetworkCredential(usr,pwd,dom);  // wont work

 

Hope it helps !!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.