NTLM身份验证使用Mechanize(Ruby)保持失败

时间:2014-08-11 08:56:46

标签: ruby mechanize ntlm

我尝试使用Mechanize gem登录到使用NTLM进行域身份验证的Intranet网站,并从中获取一些信息。但是,以下代码始终生成' 401 =>网:: HTTPUnauthorized'错误:

require 'rubygems'
require 'mechanize'
require 'logger'

agent = Mechanize.new { |a| a.log = Logger.new("test.log") }
agent.user_agent_alias = "Mac Safari"
agent.add_auth('http://intranet.website.com/', 'Username', 'Password')


result = agent.get('http://intranet.website.com/JobList.aspx')
puts result.body

我尝试了很多add_auth变体,包括使用' DOMAIN \ Username'指定域名。和' Domain = Domain',但我登录时,该网站通常不会在我的Mac上提示该信息。

日志信息的相关部分如下:

I, [2014-08-11T01:42:40.714137 #30914]  INFO -- : Net::HTTP::Get: /JobList.aspx
D, [2014-08-11T01:42:40.714187 #30914] DEBUG -- : request-header: accept-encoding =>      gzip,deflate,identity
D, [2014-08-11T01:42:40.714222 #30914] DEBUG -- : request-header: accept => */*
D, [2014-08-11T01:42:40.714256 #30914] DEBUG -- : request-header: user-agent =>  Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/534.51.22 (KHTML, like Gecko)  Version/5.1.1 Safari/534.51.22
D, [2014-08-11T01:42:40.714290 #30914] DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
D, [2014-08-11T01:42:40.714322 #30914] DEBUG -- : request-header: accept-language => en-us,en;q=0.5
D, [2014-08-11T01:42:40.714355 #30914] DEBUG -- : request-header: host => intranet.website.com
D, [2014-08-11T01:42:40.714388 #30914] DEBUG -- : request-header: authorization => NTLM TlRMTVNTUAABAAAAB4IIAA==
I, [2014-08-11T01:42:40.965768 #30914]  INFO -- : status: Net::HTTPUnauthorized 1.1 401 Unauthorized
D, [2014-08-11T01:42:40.965875 #30914] DEBUG -- : response-header: content-length => 1539
D, [2014-08-11T01:42:40.965926 #30914] DEBUG -- : response-header: content-type => text/html
D, [2014-08-11T01:42:40.965969 #30914] DEBUG -- : response-header: server => Microsoft-IIS/6.0
D, [2014-08-11T01:42:40.966011 #30914] DEBUG -- : response-header: www-authenticate => NTLM TlRMTVNTUAACAAAAEgASADgAAAAFgokCoX8cqyBcrwAAAAAAAAAAAJYAlgBKAAAABQLODgAAAA9SAEIAQQBNAE8AVQBTAEUAUgACABIAUgBCAEEATQBPAFUAUwBFAFIAAQAQAFIASwBBAE0AUwAyADUAMQAEABwAZQBtAGUAYQAuAHIAbwBjAGgAZQAuAGMAbwBtAAMALgByAGsAYQBtAHMAMgA1ADEALgBlAG0AZQBhAC4AcgBvAGMAaABlAC4AYwBvAG0ABQASAHIAbwBjAGgAZQAuAGMAbwBtAAAAAAA=
D, [2014-08-11T01:42:40.966054 #30914] DEBUG -- : response-header: x-powered-by => ASP.NET
D, [2014-08-11T01:42:40.966096 #30914] DEBUG -- : response-header: date => Mon, 11 Aug 2014 08:42:39 GMT
D, [2014-08-11T01:42:40.966194 #30914] DEBUG -- : Read 860 bytes (860 total)
D, [2014-08-11T01:42:40.967109 #30914] DEBUG -- : Read 679 bytes (1539 total)
I, [2014-08-11T01:42:40.968604 #30914]  INFO -- : Net::HTTP::Get: /JobList.aspx

有关为什么会失败的任何建议?我也可以尝试其他任何基于红宝石的替代品。

谢谢, d

编辑:我已经尝试了以下问题中建议的解决方案无济于事:Possible duplicate我相信这可能是因为我使用的是Mechanize(2.7.3)&的不同版本。 Ruby(2.0.0)

0 个答案:

没有答案