简体   繁体   English

PowerShell-Windows可信证书无法通过FTP验证SSL

[英]PowerShell - Windows Trusted Certificate not Authenticating SSL over FTP

I complete steps 1-4 of this answer , which adds my certificate to the "Trusted Root Certification Authorities" > "Certificates," and the certificate is granted <All> intended purposes. 我完成了此答案的步骤1-4,将我的证书添加到“受信任的根证书颁发机构”>“证书”,并且授予了<All>预期目的该证书。

Executing the below PowerShell code fails with The remote certificate is invalid according to the validation procedure when $ftp_request.EnableSsl = $true . 执行以下PowerShell代码失败并The remote certificate is invalid according to the validation procedure $ftp_request.EnableSsl = $true时, The remote certificate is invalid according to the validation procedureThe remote certificate is invalid according to the validation procedure It succeeds when $ftp_request.EnableSsl = $false . $ftp_request.EnableSsl = $false时,它成功。

$file_folder = "C:\Users\username\Desktop"
$file_name = "test.txt"
$file_path = "$file_folder\$file_name"
$ftp_path = "ftp://127.0.0.1/$file_name"

$username = "user"
$pwd = "pass"

# Create a FTPWebRequest object to handle the connection to the ftp server
$ftp_request = [System.Net.FtpWebRequest]::Create($ftp_path)

# set the request's network credentials for an authenticated connection
$ftp_request.Credentials =
    New-Object System.Net.NetworkCredential($username, $pwd)

$ftp_request.UseBinary = $true
$ftp_request.UsePassive = $true
$ftp_request.KeepAlive = $false

$ftp_request.EnableSsl = $true

$ftp_request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile

$file_contents = Get-Content -en byte $file_path
$ftp_request.ContentLength = $file_contents.Length

$ftp_stream = $ftp_request.GetRequestStream()
$ftp_stream.Write($file_contents, 0, $file_contents.Length)
$ftp_stream.Close()
$ftp_stream.Dispose()

I know that it's possible to manually handle this by writing a handler to ServicePointManager.ServerCertificateValidationCallback , but I would like to have SSL certificates handled automatically by the Windows cert manager. 我知道可以通过将处理程序写入ServicePointManager.ServerCertificateValidationCallback来手动处理此问题,但是我想让Windows证书管理器自动处理SSL证书。

 $ftp_path = "ftp://127.0.0.1/$file_name" 

Adding a certificate as trusted for all purposes does not mean that a certificate is trusted for all hosts. 为所有目的将证书添加为受信任并不意味着证书对所有主机都是受信任的。 The hostname you use to connect still has to match the subject of the certificate. 您用于连接的主机名仍必须与证书的主题匹配。 And while you don't provide any information about the certificate itself my guess is that your certificate is not issued for the subject "127.0.0.1". 而且,尽管您不提供有关证书本身的任何信息,但我想您的证书并未针对主题“ 127.0.0.1”签发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM