Home  >  Article  >  Backend Development  >  How can I use system credentials for NTLM authentication in Go HTTP requests?

How can I use system credentials for NTLM authentication in Go HTTP requests?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-25 21:24:02950browse

How can I use system credentials for NTLM authentication in Go HTTP requests?

NTLM Authentication in Go HTTP Requests Using System Credentials

Authenticating with NTLM in a Go HTTP request can be simplified by utilizing the user's system credentials. In other languages like C# and Python, this is achievable using built-in libraries.

To perform NTLM authentication with system credentials in Go, you can leverage the go-ole package. This package allows interaction with COM (Component Object Model) objects, including the WinHTTPRequest object.

Here's a code snippet that demonstrates 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>

By leveraging the WinHTTPRequest object and setting the AutoLogonPolicy to 0, the system credentials are automatically applied to the HTTP request. This approach simplifies NTLM authentication in Go, allowing you to make requests without explicitly providing username and password credentials.

The above is the detailed content of How can I use system credentials for NTLM authentication in Go HTTP requests?. 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