HTTP基本身份验证 - 预期的浏览器体验是什么?

时间:2010-01-11 19:29:43

标签: curl http-basic-authentication

当服务器允许通过基本HTTP身份验证进行访问时,浏览器的预期体验是什么?

我通常只使用curl执行此操作:

curl -u myusername:mypassword http://somesite.com

它运作得很好。但是,现在我无法访问curl(长篇故事),如果可能的话,我想从Web浏览器中进行操作。

我认为Basic Auth应该工作的方式是 - 我输入我想要的网址,服务器然后决定我没有被授权,返回响应代码401,我输入我的用户名和密码到提示符。如果它是正确的,页面加载!

然而,在somesite.com上,我根本没有获得授权提示,只是一个页面上写着我没有被授权。有些网站没有正确实现Basic Auth工作流程,还是我还需要做其他事情?

6 个答案:

答案 0 :(得分:142)

为了帮助每个人避免混淆,我将分两部分重新提出问题。

首先:“如何使用BASIC身份验证使用浏览器进行经过身份验证的HTTP请求?”

在浏览器中,您可以先等待提示来进行http基本身份验证,也可以按照以下格式修改网址:http://myusername:mypassword@somesite.com

注意:如果您安装了命令行和curl,问题中提到的curl命令就完全没问题了。 ;)

参考文献:

同样根据CURL手册页https://curl.haxx.se/docs/manual.html

HTTP

  Curl also supports user and password in HTTP URLs, thus you can pick a file
  like:

      curl http://name:passwd@machine.domain/full/path/to/file

  or specify user and password separately like in

      curl -u name:passwd http://machine.domain/full/path/to/file

  HTTP offers many different methods of authentication and curl supports
  several: Basic, Digest, NTLM and Negotiate (SPNEGO). Without telling which
  method to use, curl defaults to Basic. You can also ask curl to pick the
  most secure ones out of the ones that the server accepts for the given URL,
  by using --anyauth.

  NOTE! According to the URL specification, HTTP URLs can not contain a user
  and password, so that style will not work when using curl via a proxy, even
  though curl allows it at other times. When using a proxy, you _must_ use
  the -u style for user and password.

第二个也是真正的问题是“但是,在somesite.com上,我根本没有得到授权提示,只是一个页面说明我没有被授权。有些网站没有实现Basic Auth工作流程正确的,或者还有其他我需要做的事情吗?“

curl文档说-u选项支持许多身份验证方法,Basic是默认方法。

答案 1 :(得分:63)

你尝试过吗?

curl somesite.com --user username:password

答案 2 :(得分:14)

您的浏览器中可能存在旧的无效用户名/密码。尝试清除它们并再次检查。

如果您使用IE并且somesite.com位于您的Intranet安全区域,则IE可能会自动发送您的Windows凭据。

答案 3 :(得分:7)

WWW-Authenticate标头

如果服务器正在发送401响应代码但没有正确设置WWW-Authenticate标头,你也可以得到这个 - 我应该知道,我刚刚修改了自己的代码,因为VB应用程序没有弹出身份验证提示。

答案 4 :(得分:6)

如果请求标头中没有提供凭据,则以下是IE提示用户提供凭据并重新提交请求所需的最低响应。

Response.Clear();
Response.StatusCode = (Int32)HttpStatusCode.Unauthorized;
Response.AddHeader("WWW-Authenticate", "Basic");

答案 5 :(得分:5)

你可以使用Postman一个chrome插件。 它使您能够为每个请求选择所需的身份验证类型。 在该菜单中,您可以配置用户和密码。 邮递员会自动将配置转换为将随您的请求一起发送的身份验证标头。