SSL Certificate Issue - The remote certificate is

2019-05-11 10:55发布

问题:

I'm getting the following error when trying to upload a file to my server via C# desktop application: "The remote certificate is invalid according to the validation procedure." This has something to do with the SSL Certificate. It's for my website which is hosted by Arvixe. Here is the code I use:

        public void Upload(string strFileToUpload)
    {
        FileInfo fiToUpload = new FileInfo(strFileToUpload);
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.sd.arvixe.com/" + strDomain + "/wwwroot/OnlineGalleries/" + strOnlineGalleryName + "/Gallery/" + fiToUpload.Name);
        request.EnableSsl = true;
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential("usr", "psw");
        Stream sFTP = request.GetRequestStream();
        FileStream file = File.OpenRead(strFileToUpload);
        int length = 1024;
        byte[] buffer = new byte[length];
        int bytesRead = 0;
        do
        {
            bytesRead = file.Read(buffer, 0, length);
            sFTP.Write(buffer, 0, bytesRead);
        }
        while (bytesRead != 0);
        file.Close();
        sFTP.Close();
    }

This code works fine if I set request.EnableSsl = false. I'm not sure what to do. I've tried setting the URI to ftps:// and sftp://, but they return the error "The URI prefix is not recognized." any help is appreciated.

回答1:

Hard to test your code as it connects to a public service. But if you just want to ignore the SSL error perhaps this can help you do that:

System.Net.ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;


标签: c# ssl ftp