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 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建