"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Perform NTLM Authentication in Go HTTP Requests with System Credentials?

How to Perform NTLM Authentication in Go HTTP Requests with System Credentials?

Published on 2024-11-16
Browse:489

How to Perform NTLM Authentication in Go HTTP Requests with System Credentials?

NTLM Authentication in Go HTTP Requests with System Credentials

In this question, the user seeks guidance on performing Windows NTML authentication in a Go HTTP request using the system credentials of the calling user. They provide examples from C# and Python that demonstrate how to achieve this in those languages.

The solution lies in utilizing the go-ole library, which enables the use of WinHTTPRequest in Go. By following a similar approach as the Python example, it is possible to implement NTML authentication with system credentials in Go.

Here is the provided code snippet in Go that accomplishes 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 WinHTTPRequest object, sets the auto logon policy to use the current user's credentials, opens a GET request to the specified URL, sends the request, and retrieves the response text.

Latest tutorial More>

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