Windows System Credentials in Go HTTP NTLM Requests: A Solution with Go-OLE
To perform NTLM authentication in a Go HTTP request using the system credentials of the Windows user, consider the following approach:
Leveraging Go's support for COM interoperability, it's possible to utilize the WinHTTPRequest object to establish an HTTP connection with NTLM authentication. By leveraging the go-ole package, this can be achieved as follows:
<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>
By utilizing the go-ole package to interact with the WinHTTPRequest object, this code snippet provides a solution to perform NTLM authentication using the system credentials of the Windows user, without the need to manually specify a username or password.
The above is the detailed content of How to Authenticate Go HTTP NTLM Requests with Windows System Credentials?. For more information, please follow other related articles on the PHP Chinese website!