NTLM和Golang

huangapple go评论84阅读模式
英文:

NTLM and Golang

问题

我最近学习了Go语言,并且正在尝试使用net/http库进行实验。我一直在尝试使用http.SetBasicAuth函数进行站点身份验证,但似乎从未成功。使用cURL时可以正常工作,但在Go中不行。我知道这与NTLM有关,但我不知道如何解决这个问题。

cURL:

curl -v "http://server_that_im_trying_to_auth_with" --ntlm -u user:pass

Go:

req, _ := http.NewRequest("GET", "url", nil)
req.SetBasicAuth(user, pass)
resp, _ := http.DefaultClient.Do(req)
body, _ := ioutil.ReadAll(resp.Body)

返回的body一直是站点的401页面。

英文:

I have recently learnt Go and I am experimenting with the net/http library. I have been trying to authenticate into a site using the http.SetBasicAuth function but it never seems to work. It works fine with cURL but not with Go. I know this has something to do with NTLM but I don't know how to fix the problem

cURL:

curl -v "http://server_that_im_trying_to_auth_with" --ntlm -u user:pass

Go:

 req, _ := http.NewRequest("GET", "url", nil)
 req.SetBasicAuth(user, pass)
 resp, _ := http.DefaultClient.Do(req)
 body, _ := ioutil.ReadAll(resp.Body)

The body keeps on returning the sites 401 Page.

答案1

得分: 6

NTLM身份验证和基本身份验证不同。NTLM是一个比仅在标头中使用用户:密码字符串更复杂的协议。
如果您想要从Golang代码发出身份验证请求,您应该使用现有的库之一,例如:go-ntlmssp

您还可以在此处了解有关NTLM协议本身的更多信息。

英文:

NTLM Authentication and Basic Authentication are not the same. NTLM is a protocol which is more complicated than just user:password string in header.
If you want to make a request from Golang code to authenticate you should use one of existing libraries, like: go-ntlmssp

Also you can read more about NTLM protocol itself here

huangapple
  • 本文由 发表于 2017年7月29日 16:24:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/45387108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定