問題:
クロスドメインajax リクエスト (CORS)を介して、基本認証で保護されているページ (securePage.html) を読み込もうとしています。
基本認証 (カスタムヘッダー) + CORS = プリフライト OPTIONS-Request!
認証情報は実際のリクエストに提供されますが、プリフライト リクエストはそれらを送信しません!
応答: 401 Unauthorized!
ル環境:
サーバー: IIS 8.5 は私の securePage.html をホストします。これは私の web.config です。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:4444" />
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Allow-Methods" value="GET,POST" />
<add name="Access-Control-Allow-Headers" value="Origin, Content-Type, Accept, Authorization" />
</customHeaders>
</httpProtocol>
// **Authorization rules: only Bob is allowed to view the page, everyone is allowed to send an OPTIONS request.**
<security>
<authorization>
<remove users="*" roles="" verbs="" />
<add accessType="Allow" users="*" verbs="OPTIONS" />
<add accessType="Allow" users="Bob" />
</authorization>
</security>
</system.webServer>
</configuration>
ローカル PC ( http://localhost:4444 ) から ajax リクエストを送信します。
function load(){
$.ajax({
type: "GET",
cache: false,
url: "http://[MY_DOMAIN]/secureContent/securePage.html",
contentType: "text/plain; charset=UTF-8", /* Note: For cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plain will trigger the browser to send a preflight OPTIONS request to the server.*/
xhrFields: {
withCredentials: true
},
headers:{
'Authorization': 'Basic ' + btoa('Bob:[MY_PASSWORD]') /*Setting custom headers to XHR triggers a preflight request*/
},
success: function(data){
console.log('success');
alert(data);
},
error: function(jqXhr, textStatus, errorThrown){
console.log('error');
alert(jqXhr.responseText);
},
});
}
質問:
どうやら承認ルールは何もしないようです! なんで?私は試した:
- setting a request filtering (allow OPTIONS);
- giving read access to OPTINOSVerbHanlder;
成功なし!
この問題の解決策はありますか? ここで何か見逃しましたか?
ル ソリューション:
//TODO...