千万别入了 header(www-authenticate)大小写的坑

本文发布于 5 年前,部分内容可能已经失去参考价值。

ASP.NET Web API 返回 Unauthorized 401 未授权代码:

return Unauthorized(new AuthenticationHeaderValue("getUserInfo"));

响应的头部信息是这样的:

image.png

以微信小程序中获取 header 的 www-authenticate 为例,不同操作系统中获取的 key 是不一样的:


iOS:

image.png

Android:

image.png

微信开发者工具:

微信图片_20190716125001.png

所以,小程序端获取该值,暂时使用以下代码吧:

let www_authenticate;
if (!www_authenticate) {
	www_authenticate = res.header['Www-Authenticate']; // iOS
}
if (!www_authenticate) {
	www_authenticate = res.header['WWW-Authenticate']; // Android / 微信开发者工具
}
if (!www_authenticate) {
	www_authenticate = res.header['www-authenticate']; // ASP.NET Web API 原始输出
}
if (!www_authenticate) {
	www_authenticate = res.header['WWW-AUTHENTICATE']; // 其它情况
}
xoyozo 5 年前
转载请注明出处
云服务器 精选特惠
可能相关的内容