【问题标题】:understanding basic authentication with a 401了解 401 的基本身份验证
【发布时间】:2012-12-09 22:49:36
【问题描述】:
关于网络浏览器的基本身份验证,我有点困惑。我原以为 Web 浏览器只会在上一个响应中收到 HTTP 401 状态后才发送 Authorization 标头。但是,Chrome 似乎在此后的每个请求中都发送了 Authorization 标头。它包含我曾经输入的数据,以响应我网站上的 401,并与每条消息一起发送(根据 Chrome 和我的网络服务器附带的开发人员工具)。这是预期的行为吗?是否有一些我应该与我的 401 一起使用的标头来推断不应缓存授权内容?我目前正在使用 WWW-Authenticate 标头。
【问题讨论】:
标签:
iis
http-headers
wcf-data-services
basic-authentication
http-status-code-401
【解决方案1】:
这是RFC 2617 (Section 2) 中定义的浏览器的预期行为:
A client SHOULD assume that all paths at or deeper than the depth of
the last symbolic element in the path field of the Request-URI also
are within the protection space specified by the Basic realm value of
the current challenge. A client MAY preemptively send the
corresponding Authorization header with requests for resources in
that space without receipt of another challenge from the server.
Similarly, when a client sends a request to a proxy, it may reuse a
userid and password in the Proxy-Authorization header field without
receiving another challenge from the proxy server. See section 4 for
security considerations associated with Basic authentication.
据我所知,基本 HTTP 身份验证无法执行注销/重新身份验证。再加上 HTTP 基本身份验证缺乏安全性,这就是为什么现在大多数网站都使用表单和 cookie 作为身份验证解决方案的原因。
【解决方案2】:
来自RFC 2617:
如果先前的请求已获得授权,则
相同的凭证可以重复用于其中的所有其他请求
保护空间的时间段由确定
身份验证方案、参数和/或用户偏好。
根据我的经验,浏览器会自动为后续请求发送基本凭据是很常见的。它避免了为额外资源进行额外的往返。