首页  >  文章  >  后端开发  >  如何使用 Windows 系统凭据验证 Go HTTP NTLM 请求?

如何使用 Windows 系统凭据验证 Go HTTP NTLM 请求?

Barbara Streisand
Barbara Streisand原创
2024-10-24 19:11:02117浏览

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

Go HTTP NTLM 请求中的 Windows 系统凭据:使用 Go-OLE 的解决方案

使用以下方式在 Go HTTP 请求中执行 NTLM 身份验证Windows 用户的系统凭据,请考虑以下方法:

利用 Go 对 COM 互操作性的支持,可以利用 WinHTTPRequest 对象建立具有 NTLM 身份验证的 HTTP 连接。通过利用 go-ole 包,可以按如下方式实现:

<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>

通过利用 go-ole 包与 WinHTTPRequest 对象交互,此代码片段提供了一种使用以下方式执行 NTLM 身份验证的解决方案: Windows 用户的系统凭据,无需手动指定用户名或密码。

以上是如何使用 Windows 系统凭据验证 Go HTTP NTLM 请求?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn