FtpWebRequest错误。远程服务器返回错误:(530)未登录 [英] FtpWebRequest Errror. The remote server returned an error: (530) Not logged in
问题描述
我有以下代码,并给出错误"远程服务器返回错误:(530)未登录"。 System.Net.WebException的Response部分说"530没有提供客户端证书。"。 所以,它与我做错了的证书有关
的东西。代码如下所示。
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;
X509证书证书=新X509Certificate(CertificatePath,CertificatePassKey.Normalize());
//创建证书集合对象并向其添加创建的证书。
X509CertificateCollection certCollection = new X509CertificateCollection();
certCollection.Add(证书);
//为FTPWebRequest对象设置SSL设置
ftpRequest.EnableSsl = true;
ftpRequest.ClientCertificates = certCollection;
使用(FtpWebResponse ftpResponse =(FtpWebResponse)ftpRequest.GetResponse())
{
}
有一个名为WINSCP的第三方工具允许我们使用以下代码进行连接。据我所知,它是一个高级库,我们不知道下面是什么,但知道这个第三方库正在工作并且
i可能在我上面的.NET代码中犯了一些错误令人沮丧。
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#"
};
使用(Session session = new Session())
{
//连接
session.Open(sessionOptions);
//您的代码
}
真的很感激一些帮助。
嗨TarunPnkl,
感谢您发布此处。
对于您的问题,我创建了一个FTP来访问。当您使用 FtpWebRequest通过Credentials连接到ftp时,只需使用NetworkCredential(UserName,PassWord)。
//获取对象用于与服务器通信。
FtpWebRequest request =(FtpWebRequest)WebRequest.Create(" ftp:// localhost /");
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(" UserName"," PassWord");
FtpWebResponse response =(FtpWebResponse)request.GetResponse();
流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();它适用于我。请检查代码。如果您仍然有登录问题,请检查您的应用程序以及UserName和PassWord的许可。
最好的问候,
Wendy
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.
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
这篇关于FtpWebRequest错误。远程服务器返回错误:(530)未登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!