-
FTP File Transfer
I am trying to access a FTP secured site (SSL/TSL) and transfer a file in VB.Net using FTPRequest. When I try to complete the transfer I get an error back "The remote certificate is invalid according to the validation procedure".
However when I connect to the same FTP site with FileZilla UI, I can transfer files with no problem. Why can do transfers with FileZilla and not in VB.Net? Does FileZilla have its own Certificate?
-
Re: FTP File Transfer
Can you show us the code you're using?
-
Re: FTP File Transfer
here is the code: I have replace the siteid, userid and password from the code.
Dim URI As String = "FTP://<ftpsite.com>"
Dim UserName As String = "<userid>"
Dim Password As String = "<userpsw>"
Dim WResp As FtpWebResponse
Dim Wreq As FtpWebRequest = DirectCast(FtpWebRequest.Create(URI & "/test.txt"), FtpWebRequest)
Try
Wreq.EnableSsl = True
Wreq.KeepAlive = True
Wreq.UsePassive = True
Wreq.Method = WebRequestMethods.Ftp.UploadFile
Wreq.Credentials = New System.Net.NetworkCredential(UserName, Password)
Dim bFile() As Byte = System.IO.File.ReadAllBytes("f:\ftpud.txt")
' upload file...
****** it is here when I try the GetRequestStream that I get the error *****
Dim clsStream As System.IO.Stream = Wreq.GetRequestStream()
clsStream.Write(bFile, 0, bFile.Length)
clsStream.Close()
clsStream.Dispose()
WResp = Wreq.GetResponse()
Catch ew As WebException
MsgBox(ew.Message)
Catch e As Exception
MsgBox(e.Message)
End Try
-
Re: FTP File Transfer
You need to register a callback that performs the certificate lookup/validation. I'm afraid I only have a C# example but something like:
Code:
// Register a callback function to validate the server's X509
// certificate for SSL
System.Net.ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(ValidateServerCertificate);
where the callback function is:-
Code:
/// <summary>
/// Validates a servers X509 certificate
/// </summary>
/// <param name="sender"></param>
/// <param name="certificate"></param>
/// <param name="chain"></param>
/// <param name="sslPolicyErrors"></param>
/// <returns></returns>
public static bool ValidateServerCertificate(object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors ==
SslPolicyErrors.RemoteCertificateChainErrors)
{
return false;
}
else if (sslPolicyErrors ==
SslPolicyErrors.RemoteCertificateNameMismatch)
{
System.Security.Policy.Zone z =
System.Security.Policy.Zone.CreateFromUrl
(((WebRequest)sender).RequestUri.ToString());
if (z.SecurityZone ==
System.Security.SecurityZone.Intranet ||
z.SecurityZone ==
System.Security.SecurityZone.MyComputer)
{
return true;
}
return false;
}
return true;
}