Home  >  Article  >  Backend Development  >  How to Authenticate Go HTTP NTLM Requests with Windows System Credentials?

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

Barbara Streisand
Barbara StreisandOriginal
2024-10-24 19:11:02117browse

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

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn