5

I am trying to login to a ftp server. Using the following code in C#.

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp-server");

request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
// This example assumes the FTP site uses anonymous logon.
//request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequired;
request.Credentials = new NetworkCredential("userName", "password!#£");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();

However the login fails when the password contains some special charaters. e.g ('!' or '£')? I get the following exception.

Unhandled Exception: System.Net.WebException: The remote server returned an error: (530) Not logged in.
  at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
  at System.Net.FtpWebRequest.RequestCallback(Object obj)
  at System.Net.CommandStream.Dispose(Boolean disposing)
  at System.IO.Stream.Close()
  at System.IO.Stream.Dispose()
  at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
  at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Bo
ean canReuse)
  at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
  at System.Net.FtpWebRequest.GetResponse()
  at FtpClientTest.Program.FtpWebRequest() 

I don't get any exception when the password does not contain any special characters.

Amitabh
  • 59,111
  • 42
  • 110
  • 159
  • Why are you UTF-8 encoding the password? What happens if you just pass it as an native string? – Joe Dec 01 '11 at 12:31
  • does `doesEncodeUTF8("xxxxxxx")` retrun escaped ascii string?? try this may it will solve the problem.. [Converting Unicode strings to escaped ascii string](http://stackoverflow.com/questions/1615559/converting-unicode-strings-to-escaped-ascii-string) I have explored all classes of `NetworkCredential Class` but all uses string type password, event `secureString` also have same char/string type values.. i am agree with @Joe that you should native string type secure password.. – Niranjan Singh Dec 01 '11 at 12:49
  • 1
    @Joe: I tried first the native string and it did not work. Then I tried using UTF8 encoding which also dis not work. – Amitabh Dec 01 '11 at 17:17
  • @Niranjan: I tried the to EncodeNonAsciChars and that also did not work. – Amitabh Dec 01 '11 at 17:19
  • Trace Ftp Log to check whether this problem regarding server or some other. check this link [How to troubleshoot your System.Net code](http://blogs.msdn.com/b/ncl/archive/2008/07/25/how-to-troubleshoot-your-system-net-code.aspx) – Niranjan Singh Dec 02 '11 at 06:18

1 Answers1

1

what ftp server is it? can you login using other programs? if yes, use wireshark to compare what goes over the wire.

So it is probably just ANSI encoding, so try

var secureString = new SecureString();
foreach (var b in Encoding.Default.GetBytes("password!#£"))
    secureString.AppendChar((char)b);

request.Credentials = new NetworkCredential("userName", secureString);
esskar
  • 10,638
  • 3
  • 36
  • 57