2015-12-28 47 views
1

我完成了this answer的步驟1-4,它將我的證書添加到「受信任的根證書頒發機構」>「證書」中,並且該證書被授予<All>的預期用途。PowerShell - Windows可信證書不通過FTP驗證SSL

執行下面的PowerShell代碼時出現失敗。 $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() 

我知道,它可以通過寫一個處理程序ServicePointManager.ServerCertificateValidationCallback手動處理這個問題,但我想有由Windows證書管理器自動處理SSL證書。

回答

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

添加證書作爲值得信賴的所有目的,並不意味着一個信任證書進行的所有主機。您用於連接的主機名仍必須與證書的主題相匹配。雖然您沒有提供有關證書本身的任何信息,但我的猜測是您的證書未針對主題「127.0.0.1」發佈。