在开发一个基本身份验证的IIS模块时,我遇到了问题。当浏览网页时,该模块运行良好,但是调用Web服务时似乎请求没有到达模块,并且一些中间模块控制了该请求。
通过使用Fiddler,我发现当HTTP请求头中的Content-type设置为application/json时,中间模块/处理程序会触发。所以以下请求不起作用:
当正常工作时,服务器应该要求客户端通过在响应中设置WWW-Authenticate标头发送用户凭据。
(注:原文中的英文单词和标点符号已经正确翻译,保留了HTML标签)
通过使用Fiddler,我发现当HTTP请求头中的Content-type设置为application/json时,中间模块/处理程序会触发。所以以下请求不起作用:
当正常工作时,服务器应该要求客户端通过在响应中设置WWW-Authenticate标头发送用户凭据。
GET /WebServices/service.asmx/someMethod?param=test HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Content-Type: application/json
asdfasdf
asdfasdfasdf
响应:请注意响应中的jsonerror标头
HTTP/1.1 401 Unauthorized
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
jsonerror: true
X-Powered-By: ASP.NET
Date: Mon, 11 Mar 2013 23:49:02 GMT
Content-Length: 105
{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.In
validOperationException"}
这个可以正常工作:请注意没有content-type
(注:原文中的英文单词和标点符号已经正确翻译,保留了HTML标签)
GET /WebServices/service.asmx/someMethod?param=test HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
asdfasdf
asdfasdfasdf
正确的回应是:注意响应中的WWW-Authenticate
头
HTTP/1.1 401 Unauthorized
Location: http://localhost/WebServices/service.asmx/someMethod?param=test
Server: Microsoft-IIS/7.5
WWW-Authenticate: Basic
X-Powered-By: ASP.NET
Date: Mon, 11 Mar 2013 23:59:48 GMT
Content-Length: 0