C#でFtpWebRequest
クラスを使用しているときに問題が発生しました。FtpWebRequestは、正しいパスワードを使用して正常に接続した後に間違ったパスワードを無視します。
初めて正しい資格情報を使用してFTPサーバーにファイルをアップロードしようとしたときに、2回目の間違った資格情報(ユーザー名は同じだが間違ったパスワード)が発生した場合、例外はスローされず、ファイルは依然として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("[email protected]", "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("[email protected]", "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();
}
}
ここで私は、私はここにあなたがこのコードをテストするために使用できるhttps://dlptest.com/ftp-test/を発見した公共ftpサーバftp://ftp.dlptest.com/
を使用しています。
最初に、正しい資格情報を使用してファイルをアップロードしてから、同じユーザー名を使用してパスワードを変更して間違った資格情報を使用してファイルをアップロードしようとします。 ファイルはまだサーバーにアップロードされています。最初に間違った資格情報を使用しようとすると、例外がスローされ、すべてが期待通りに機能します。
あなたは何が起こっているのか分かりますか?これはフレームワークのバグですか?私が今作業しているプログラムに問題が生じるので、この問題に対処する私の選択肢は何ですか?