iT邦幫忙

0

POWERSHELL 呼叫WEB API ,無法為 SSL/TLS 安全通道建立信任關係

  • 分享至 

  • xImage

您好:

參考
https://igouist.github.io/post/2021/05/newbie-2-webapi/
大作

執行後,網頁可以看到API內容

但用
使用powershell 呼叫
Invoke-RestMethod https://localhost:7270/weatherforecast | ConvertTo-Json
卻出現

Invoke-RestMethod : 基礎連接已關閉: 無法為 SSL/TLS 安全通道建立信任關係。
位於 線路:1 字元:1
+ Invoke-RestMethod https://localhost:7270/weatherforecast | ConvertTo- ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod],WebExce
    ption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

查看
https://blog.miniasp.com/post/2019/01/12/PowerShell-Invoke-WebRequest-use-TLS-v12
有去
C:\Users\XX\Documents\WindowsPowerShell
建立一個 Microsoft.PowerShell_profile.ps1 填入 [System.Net.ServicePointManager]::SecurityProtocol = "tls12,tls11" 等
一樣無效

請問 這是否解決方法?

建立專案時,只有NET6.0長期穩定可選
另外,執行時沒有使用建立SSL憑證
https://ithelp.ithome.com.tw/upload/images/20231221/20104095gLUza4yNSW.png

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
Daniel
iT邦新手 2 級 ‧ 2023-12-22 22:35:37
最佳解答

如果你用的是新版 PowerShell,可以加上 -SkipCertificateCheck 參數略過憑證檢查
參考:Invoke-RestMethod

看更多先前的回應...收起先前的回應...
noway iT邦研究生 3 級 ‧ 2023-12-24 19:23:36 檢舉

您好:
但一樣出現

Invoke-RestMethod https://localhost:7270/weatherforecast | ConvertTo-Json -SkipCertificateCheck
ConvertTo-Json : 找不到符合參數名稱 'SkipCertificateCheck' 的參數。
位於 線路:1 字元:76
+ ... localhost:7270/weatherforecast | ConvertTo-Json -SkipCertificateCheck
+                                                     ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [ConvertTo-Json],ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ConvertToJsonCommand
Daniel iT邦新手 2 級 ‧ 2023-12-24 23:30:50 檢舉

-SkipCertificateCheck 是 Invoke-RestMethod 的參數,不是 ConvertTo-Json 的參數

noway iT邦研究生 3 級 ‧ 2023-12-25 22:57:23 檢舉

Invoke-RestMethod -SkipCertificateCheck https://localhost:7270/weatherforecast | ConvertTo-Json

結果仍是
Invoke-RestMethod : 找不到符合參數名稱 'SkipCertificateCheck' 的參數。

Daniel iT邦新手 2 級 ‧ 2023-12-26 09:23:57 檢舉

你是用 Windows 內建的 PowerShell 嗎?內建的 Invoke-RestMethod 不支援這個參數,你要找新版,目前版號是 7.4.0

noway iT邦研究生 3 級 ‧ 2023-12-26 22:10:00 檢舉

您好:我的是WIN 10 22H2 內建的pwoershell
剛用 $PSVersionTable 看
5.1.19041.3803 版
所以下載安裝7.4版

Invoke-RestMethod -SkipCertificateCheck http://localhost:5270/weatherforecast | ConvertTo-Json
就可以了
謝謝

0
Sujira
iT邦新手 4 級 ‧ 2023-12-22 09:44:11
noway iT邦研究生 3 級 ‧ 2023-12-24 19:34:43 檢舉

您好:
由 chrome F12 查到 目前1.2
https://ithelp.ithome.com.tw/upload/images/20231224/201040958fb6K7KF3t.png

我要發表回答

立即登入回答