NTLM Authentication with System Credentials in Go HTTP Requests
As a developer, you may encounter scenarios where you need to make HTTP requests and authenticate using the system credentials of the user running your application. In Windows environments, this can be done using NTLM (Negotiate Token Level Message) authentication. While there are solutions available in C# and Python, the implementation in Go is less straightforward.
Solution with Go-ole
After researching, we've found that the go-ole library provides access to the WinHTTPRequest interface, which can be utilized to perform NTLM authentication with system credentials. Here's how to achieve this:
<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() unknown, _ := oleutil.CreateObject("WinHTTP.WinHTTPRequest.5.1") request, _ := unknown.QueryInterface(ole.IID_IDispatch) oleutil.CallMethod(request, "SetAutoLogonPolicy", 0) oleutil.CallMethod(request, "Open", "GET", "http://example.com", false) oleutil.CallMethod(request, "Send") resp := oleutil.MustGetProperty(request, "ResponseText") fmt.Println(resp.ToString()) }</code>
This code initializes the ole library and creates a WinHTTPRequest instance. The SetAutoLogonPolicy method is called to enable the use of system credentials. Then, the request is sent with the Open and Send methods. Finally, the response text is obtained using the ResponseText property.
Conclusion
Using the go-ole library, you can leverage the WinHTTPRequest interface to perform NTLM authentication with system credentials in Go, providing a seamless approach for HTTP requests in Windows environments.
The above is the detailed content of How to Achieve NTLM Authentication with System Credentials in Go HTTP Requests?. For more information, please follow other related articles on the PHP Chinese website!