首頁  >  文章  >  後端開發  >  如何使用 Windows 系統憑證驗證 Go HTTP NTLM 請求?

如何使用 Windows 系統憑證驗證 Go HTTP NTLM 請求?

Barbara Streisand
Barbara Streisand原創
2024-10-24 19:11:02117瀏覽

How to Authenticate Go HTTP NTLM Requests with Windows System Credentials?

Go HTTP NTLM 要求中的Windows 系統憑證:使用Go-OLE 的解決方案

使用以下方式在Go HTTP 請求中執行NTLM 驗證Windows 使用者的系統憑證,請考慮以下方法:

利用Go 對COM 互通性的支持,可以利用WinHTTPRequest 物件建立具有NTLM 驗證的HTTP 連線。透過利用go-ole 套件,可以如下實現:

<code class="go">package main

import (
    "fmt"

    ole "github.com/go-ole/go-ole"
    "github.com/go-ole/go-ole/oleutil"
)

func main() {
    ole.CoInitialize(0)
    defer ole.CoUninitialize()

    // Create a WinHTTPRequest object
    unknown, _ := oleutil.CreateObject("WinHTTP.WinHTTPRequest.5.1")
    request, _ := unknown.QueryInterface(ole.IID_IDispatch)

    // Set the auto login policy to use system credentials
    oleutil.CallMethod(request, "SetAutoLogonPolicy", 0)

    // Open the request with the desired URL
    oleutil.CallMethod(request, "Open", "GET", "http://example.com", false)

    // Send the request
    oleutil.CallMethod(request, "Send")

    // Retrieve the response text
    resp := oleutil.MustGetProperty(request, "ResponseText")

    // Print the response
    fmt.Println(resp.ToString())
}</code>

透過利用go-ole 套件與WinHTTPRequest 物件交互,此程式碼片段提供了一種使用以下方式執行NTLM 驗證的解決方案: Windows 使用者的系統憑證,無需手動指定使用者名稱或密碼。

以上是如何使用 Windows 系統憑證驗證 Go HTTP NTLM 請求?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn