IIS FTP 用户和密码中包含变音字符 - 无法连接

IIS FTP with umlaut chars in user and password - cannot connect

public void FTPOps()
{
    string ftpUserID = "ftpätest";
    string ftpPassword = "sampleäpass";
    FtpWebRequest reqFTP =
      (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://<SERVERURL>:<PORT>"));
    reqFTP.UseBinary = true;
    reqFTP.KeepAlive = false;
    reqFTP.Credentials = new NetworkCredential(ftpUserID.Normalize(), ftpPassword);
    reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
    reqFTP.UsePassive = true;
    reqFTP.Proxy = null;

    var response = reqFTP.GetResponse();
}

结果

(530): Not logged in

有以下错误堆栈。

at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.Net.FtpWebRequest.RequestCallback(Object obj)
at System.Net.CommandStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at System.IO.Stream.Dispose()
at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse)
at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
at System.Net.FtpWebRequest.GetResponse()
at ConsoleApplication1.Program.FTPOps()
at ConsoleApplication1.Program.Main(String[] args)

当通过 Windows Explorer(而非 IE)访问同一 FTP 服务器时,相同的凭据有效。

有什么想法吗?

IIS 确实有问题,虽然宣布支持 UTF-8,但它不接受 UTF-8 编码的用户名。有意思的是,UTF-8密码好像没有问题

用户名需要使用旧版 "Ansi" 编码。

我认为您无法让 FtpWebRequest 对用户名使用 "Ansi" 编码。

你最好更改用户名。

如果您无法更改用户名,则必须使用另一个能够解决 IIS 错误的 FTP 客户端。

例如,当使用 UTF-8 编码的身份验证失败时,WinSCP .NET assembly 将自动回退到 "Ansi" 凭证编码。为此,默认的遗留 "Ansi" 编码在本地计算机上必须与服务器上的编码相同(在您的情况下为 Windows-1252)

// Set up session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    PortNumber = port,
    HostName = "ftp.example.com",
    UserName = "ftpätest",
    Password = "sampleäpass",
};

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Your code
}

日志示例:

> 2017-06-22 07:54:45.067 USER ftpätest
< 2017-06-22 07:54:45.067 331 Password required
> 2017-06-22 07:54:45.067 PASS sampleäpass
< 2017-06-22 07:54:45.067 530-User cannot log in.
< 2017-06-22 07:54:45.067  Win32 error:   The user name or password is incorrect. 
< 2017-06-22 07:54:45.067  Error details: An error occurred during the authentication process.
< 2017-06-22 07:54:45.067 530 End
. 2017-06-22 07:54:45.128 Login data contains non-ascii characters and server might not be UTF-8 aware. Trying local charset.
> 2017-06-22 07:54:45.128 USER ftpätest
< 2017-06-22 07:54:45.129 331 Password required
> 2017-06-22 07:54:45.129 PASS sampleäpass
< 2017-06-22 07:54:45.129 230 User logged in.

(我是WinSCP的作者)