当服务器允许通过基本 HTTP 身份验证进行访问时,在 Web 浏览器中的预期体验是什么?
暂时忽略网络浏览器,以下是使用 curl
创建基本身份验证请求的方法:
curl -u myusername:mypassword http://somesite.example
但是在 Web 浏览器中呢?我在一些网站上看到的是,我访问了 URL,然后服务器返回响应代码 401。然后浏览器显示用户名/密码提示。
但是,在 somesite.example
上,我根本没有收到授权提示,只是显示我未获得授权的页面。某些站点是否未正确实施基本身份验证工作流程,还是我需要做其他事情?
为了帮助大家避免混淆,我将把问题重新表述为两部分。
首先:“如何使用 BASIC auth 使用浏览器发出经过身份验证的 HTTP 请求?”。
在浏览器中,您可以先执行 HTTP 基本身份验证,方法是等待提示出现,或者如果您遵循以下格式,则可以编辑 URL:http://myusername:mypassword@somesite.example
注意:如果您安装了命令行和 curl,则问题中提到的 curl 命令非常好。 ;)
参考:
https://en.wikipedia.org/wiki/Basic_access_authentication#URL_encoding
https://en.wikipedia.org/wiki/Uniform_Resource_Locator#Syntax
https://www.rfc-editor.org/rfc/rfc3986#page-18
同样根据 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.example
上,我根本没有收到授权提示,只是显示我未获得授权的页面。某些站点是否未正确实施基本身份验证工作流程,或者我还有什么需要做的吗?”
curl 文档说 -u
选项支持多种身份验证方法,Basic 是默认设置。
你有没有尝试过?
curl somesite.example --user username:password
您的浏览器中可能缓存了旧的无效用户名/密码。尝试清除它们并再次检查。
如果您使用的是 IE 并且 somesite.example
在您的 Intranet 安全区域中,则 IE 可能会自动发送您的 Windows 凭据。
WWW-身份验证标头
如果服务器正在发送 401 响应代码但未正确设置 WWW-Authenticate 标头,您也可能会收到此信息 - 我应该知道,我刚刚在自己的代码中修复了这个问题,因为 VB 应用程序没有弹出身份验证提示。
如果请求标头中没有提供凭据,以下是 IE 提示用户输入凭据并重新提交请求所需的最小响应。
Response.Clear();
Response.StatusCode = (Int32)HttpStatusCode.Unauthorized;
Response.AddHeader("WWW-Authenticate", "Basic");
您可以使用 Postman 的 chrome 插件。它使您能够为每个请求选择所需的身份验证类型。在该菜单中,您可以配置用户和密码。 Postman 会自动将配置转换为身份验证标头,该标头将随您的请求一起发送。