duanlumei5941 2017-03-15 17:20
浏览 172
已采纳

Go HTTP NTLM请求中的Windows系统凭据

I am looking for the path of least resistance for doing NTLM authentication in a Go HTTP request using the system credentials of the Windows user calling the application.

In C#/.NET, I would be able to achieve this through

WebRequest request = WebRequest.Create(url);
request.Credentials = CredentialCache.DefaultCredentials;
WebResponse response = request.GetResponse();
Stream receiveStream = response.GetResponseStream();

and in Python, the equivalent result can be obtained through

import win32com.client
h = win32com.client.Dispatch('WinHTTP.WinHTTPRequest.5.1')
h.SetAutoLogonPolicy(0)
h.Open('GET', url, False)
h.Send()

but I have not been able to find any resources on how to do the same thing in Go. I could of course use a library for NTLM authentication and manually provide a username/password, but the goal here is to avoid ever putting those in.

  • 写回答

1条回答 默认 最新

  • dongxin2734 2017-03-16 14:11
    关注

    After digging into it a bit further, it looks like go-ole can be utilized to make use of WinHTTPRequest in the same way as the Python example in the question. Ignoring all error catching,

    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())
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 asp.textbox后台赋值前端不能显示什么原因
  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误