261

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

暂时忽略网络浏览器,以下是如何使用以下命令创建基本身份验证请求curl

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

但是在 Web 浏览器中呢?我在一些网站上看到的是,我访问了 URL,然后服务器返回响应代码 401。然后浏览器显示用户名/密码提示。

但是,在 somesite.com 上,我根本没有收到授权提示,只是显示我未获得授权的页面。某些站点是否未正确实施基本身份验证工作流程,还是我需要做其他事情?

4

6 回答 6

162

为了帮助大家避免混淆,我将把问题重新表述为两部分。

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

在浏览器中,您可以先执行 http 基本身份验证,方法是等待提示出现,或者如果您遵循以下格式,则可以编辑 URL: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 上,我根本没有收到授权提示,只是显示我未获得授权的页面。某些网站没有正确实施基本身份验证工作流程,还是有什么问题?我还需要做什么?”

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

于 2013-10-24T13:32:38.297 回答
69

你有没有尝试过 ?

curl somesite.com --user username:password
于 2015-12-18T16:38:01.523 回答
15

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

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

于 2010-01-12T02:14:41.367 回答
9

WWW-身份验证标头

如果服务器正在发送 401 响应代码但未正确设置 WWW-Authenticate 标头,您也可能会收到此信息 - 我应该知道,我刚刚在自己的代码中修复了这个问题,因为 VB 应用程序没有弹出身份验证提示。

于 2010-04-07T16:42:29.950 回答
7

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

Response.Clear();
Response.StatusCode = (Int32)HttpStatusCode.Unauthorized;
Response.AddHeader("WWW-Authenticate", "Basic");
于 2012-10-26T02:29:53.167 回答
5

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

于 2015-11-11T12:35:26.640 回答