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

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

Barbara Streisand
Release: 2024-10-24 19:11:02
Original
219 people have browsed it

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template