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:
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())
}
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.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3