【问题标题】:Using ruby net/http library I cannot authenticate http request with basic auth going to the IIS server使用 ruby​​ net/http 库我无法通过基本身份验证访问 IIS 服务器来验证 http 请求
【发布时间】:2018-04-20 02:24:05
【问题描述】:

IIS 服务器与 Negotiate 和 NTLM 一起启用了基本身份验证。这是响应标头:

{"content-type"=>["text/html"], "server"=>["Microsoft-IIS/10.0"], "www-authenticate"=>["Negotiate", "NTLM", "Basic realm=\"my_realm\""], "x-powered-by"=>["ASP.NET"], "date"=>["Thu, 19 Apr 2018 22:14:06 GMT"], "content-length"=>["1293"]}

通过浏览器进行 NTLM 身份验证,我可以通过输入我的用户凭据成功对其进行身份验证。但是通过 Ruby 的 net/http 库提交请求并进行基本身份验证,我会为同一用户获得 401 Unauthroized。以下是我尝试提交成功获取请求的代码:

require 'net/http'
def get(username, password)
uri = URI.parse(URI)
req = Net::HTTP::Get.new(uri.path)
req.basic_auth(username, password)
http = Net::HTTP.start(uri.host, uri.port) do |http|
    resp = http.request(req)
    resp
end

从在 IIS 服务器上启用基本身份验证并通过 Ruby net/http 库提交 http 请求,我已经尝试了所有钩子并进行了足够的研究,但似乎无法通过基本身份验证对服务器进行身份验证。查看响应,我确实怀疑服务器可能期待 NTLM 或协商方案,因为它似乎只是忽略了基本身份验证。任何帮助将不胜感激!

【问题讨论】:

    标签: basic-authentication net-http iis-10 ruby-1.8.7


    【解决方案1】:

    用户名中缺少域条目。我无法使用用户名“testuser”为我的用户进行身份验证。所以附加域名是这里的诀窍。这是独家新闻,对于给定的用户名“testuser”和域“FOO”,使用 username = "FOO\testuser" 进行身份验证是有效的。

    require 'net/http'
    def self.get(username, password)
      uri = URI.parse(URI)
      req = Net::HTTP::Get.new(uri.path)
      req.basic_auth(username, password)
      http = Net::HTTP.start(uri.host, uri.port) do |http|
        resp = http.request(req)
        resp
      end
    end
    

    所以,我所做的就是用这些 args:get("FOO\\testuser", "VapTest1") 调用我的 get。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-17
      相关资源
      最近更新 更多