FtpWebRequest 在之前使用正确密码成功连接后忽略错误密码
FtpWebRequest ignores wrong password after previous successful connection using correct password
我在 C# 中使用 FtpWebRequest
class 时遇到了 运行 问题。
当我第一次尝试使用正确的凭据将文件上传到 FTP 服务器,然后第二次使用错误的凭据(用户名相同但密码错误)时,没有抛出异常并且文件仍然上传到 FTP 服务器。请考虑以下代码:
using System;
using System.Net;
internal class Program
{
private static void Main(string[] args)
{
var uri = new Uri("ftp://ftp.dlptest.com/TestFile.txt");
var method = WebRequestMethods.Ftp.UploadFile;
//Here I'm uploading the test file using correct credentials
var correctCredentials =
new NetworkCredential("dlpuser@dlptest.com", "fwRhzAnR1vgig8s");
DoFtpRequest(uri, correctCredentials, method);
//Here I'm uploading the test file using wrong credentials.
//I expect some exception to be thrown and the file not being
//uploaded to the server, neither is the case.
var wrongCredentials =
new NetworkCredential("dlpuser@dlptest.com", "WRONG_PASWORD");
DoFtpRequest(uri, wrongCredentials, method);
}
public static FtpWebResponse DoFtpRequest(
Uri uri, NetworkCredential credentials, string method)
{
var request = (FtpWebRequest)WebRequest.Create(uri);
request.Credentials = credentials;
request.Method = method;
return (FtpWebResponse)request.GetResponse();
}
}
这里我使用的是 public ftp 服务器 ftp://ftp.dlptest.com/
我在这里 https://dlptest.com/ftp-test/ 找到的,你可以用它来测试这段代码。
正如您首先看到的,我尝试使用正确的凭据上传文件,然后使用错误的凭据(使用相同的用户名但更改密码)上传文件。
但是文件仍然上传到服务器。如果我首先尝试使用错误的凭据,则会抛出异常,一切都会按预期进行。
你知道这是怎么回事吗?这是框架的错误吗?我有什么选择来处理这个问题,因为它会导致我现在正在处理的程序出现问题?
FtpWebRequest
在后台使用连接池。参见 。
该连接池的键只有主机名、端口号、用户名和可选的连接组名称。
您的第二个请求重用了第一个请求的连接,并且绝不会使用错误的密码。这是因为这两个请求使用相同的连接池,因为它们只是密码不同,密码不是密钥的一部分。
而如果您交换请求,第一个请求不会成功,它的连接会关闭并且不会进入池。第二个请求必须从一个新连接开始,它将使用正确的密码。
要隔离请求,您可以:
- 对不同的请求使用唯一的
FtpWebRequest.ConnectionGroupName
,使它们使用不同的连接池。
- 或禁用 [=15=] 以完全禁用连接池。
我在 C# 中使用 FtpWebRequest
class 时遇到了 运行 问题。
当我第一次尝试使用正确的凭据将文件上传到 FTP 服务器,然后第二次使用错误的凭据(用户名相同但密码错误)时,没有抛出异常并且文件仍然上传到 FTP 服务器。请考虑以下代码:
using System;
using System.Net;
internal class Program
{
private static void Main(string[] args)
{
var uri = new Uri("ftp://ftp.dlptest.com/TestFile.txt");
var method = WebRequestMethods.Ftp.UploadFile;
//Here I'm uploading the test file using correct credentials
var correctCredentials =
new NetworkCredential("dlpuser@dlptest.com", "fwRhzAnR1vgig8s");
DoFtpRequest(uri, correctCredentials, method);
//Here I'm uploading the test file using wrong credentials.
//I expect some exception to be thrown and the file not being
//uploaded to the server, neither is the case.
var wrongCredentials =
new NetworkCredential("dlpuser@dlptest.com", "WRONG_PASWORD");
DoFtpRequest(uri, wrongCredentials, method);
}
public static FtpWebResponse DoFtpRequest(
Uri uri, NetworkCredential credentials, string method)
{
var request = (FtpWebRequest)WebRequest.Create(uri);
request.Credentials = credentials;
request.Method = method;
return (FtpWebResponse)request.GetResponse();
}
}
这里我使用的是 public ftp 服务器 ftp://ftp.dlptest.com/
我在这里 https://dlptest.com/ftp-test/ 找到的,你可以用它来测试这段代码。
正如您首先看到的,我尝试使用正确的凭据上传文件,然后使用错误的凭据(使用相同的用户名但更改密码)上传文件。 但是文件仍然上传到服务器。如果我首先尝试使用错误的凭据,则会抛出异常,一切都会按预期进行。
你知道这是怎么回事吗?这是框架的错误吗?我有什么选择来处理这个问题,因为它会导致我现在正在处理的程序出现问题?
FtpWebRequest
在后台使用连接池。参见
该连接池的键只有主机名、端口号、用户名和可选的连接组名称。
您的第二个请求重用了第一个请求的连接,并且绝不会使用错误的密码。这是因为这两个请求使用相同的连接池,因为它们只是密码不同,密码不是密钥的一部分。
而如果您交换请求,第一个请求不会成功,它的连接会关闭并且不会进入池。第二个请求必须从一个新连接开始,它将使用正确的密码。
要隔离请求,您可以:
- 对不同的请求使用唯一的
FtpWebRequest.ConnectionGroupName
,使它们使用不同的连接池。 - 或禁用 [=15=] 以完全禁用连接池。