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

Barbara Streisand
Release: 2024-10-30 02:43:29
Original
993 people have browsed it

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

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!