(この問題をこことgithubの両方に投稿して申し訳ありませんが、これについて本当に助けが必要です..)
Thinktecture.IdentityModel.45 で認証を使用している場合、CORS (System.Web.Http.Cors) を有効にできません。
トークンのエンドポイントに問題があり、デフォルトの "/token" を使用しています。
/token を (javascript クライアントから) 呼び出すと、ブラウザーは "/token" リソースに対する "OPTION" 呼び出しと "GET" 呼び出しの 2 つの呼び出しを発行します。リクエストが正しいCORSポリシーを取得し、それが返されることを確認できるようにするためだけに、単純なPolicyProviderFactoryを使用しています
new EnableCorsAttribute("*", "*", "*");
最初の OPTION 呼び出しが機能し、GetCorsPolicyProvider がヒットします。他の呼び出しは機能しますが、GetCorsPolicyProvider はヒットせず、その結果、サーバーはすべての正しいヘッダーなどを含む 200 OK を返しますが、コンテンツの応答は空です。
ルート構成のトークン リソースに次を追加する必要がありました。そうしないと、「NOT FOUND」が表示されます。
config.Routes.MapHttpRoute(
name: "SecurityToken",
routeTemplate: "api/token");
他のすべての要求では、OPTIONS と GET の両方が CORS ポリシー プロバイダーを呼び出し、すべてが機能します。thinktecture.identitymodel と正しい応答にデバッグしました。正しいトークンが返されます。
/token リソースの GET メソッドは、CORS ポリシー プロバイダーを呼び出しません..しかし、なぜですか? CorsMessageHandler はロードされますが、呼び出されません。リクエストが CORS/CorsMessageHandler をトリガーするかどうかはどうすればわかりますか?
診断トレース:
w3wp.exe Information: 0 : Request, Method=OPTIONS, Url=https://localhost/myapp/api/token, Message='https://localhost/myapp/api/token'
w3wp.exe Information: 0 : Message='CorsPolicyProvider selected: 'System.Web.Http.Cors.EnableCorsAttribute'', Operation=MyPolicyProviderFactory.GetCorsPolicyProvider
w3wp.exe Information: 0 : Message='CorsPolicy selected: 'AllowAnyHeader: True, AllowAnyMethod: True, AllowAnyOrigin: True, PreflightMaxAge: null, SupportsCredentials: True, Origins: {}, Methods: {}, Headers: {}, ExposedHeaders: {}'', Operation=EnableCorsAttribute.GetCorsPolicyAsync
w3wp.exe Information: 0 : Message='CorsResult returned: 'IsValid: True, AllowCredentials: True, PreflightMaxAge: null, AllowOrigin: https://localhost:4433, AllowExposedHeaders: {}, AllowHeaders: {access-key,authorization}, AllowMethods: {GET}, ErrorMessages: {}'', Operation=CorsEngine.EvaluatePolicy
w3wp.exe Information: 0 : Operation=CorsMessageHandler.SendAsync, Status=200 (OK)
w3wp.exe Information: 0 : Operation=AuthenticationHandler.SendAsync, Status=200 (OK)
w3wp.exe Information: 0 : Response, Status=200 (OK), Method=OPTIONS, Url=https://localhost/myapp/api/token, Message='Content-type='none', content-length=unknown'
w3wp.exe Information: 0 : Request, Method=GET, Url=https://localhost/myapp/api/token, Message='https://localhost/myapp/api/token'
w3wp.exe Information: 0 : Operation=AuthenticationHandler.SendAsync, Status=200 (OK)
w3wp.exe Information: 0 : Response, Status=200 (OK), Method=GET, Url=https://localhost/myapp/api/token, Message='Content-type='application/json; charset=utf-8', content-length=851'
認証構成は次のようになります。
var authentication = new AuthenticationConfiguration
{
ClaimsAuthenticationManager = new ClaimsTransformer(),
RequireSsl = false,
EnableSessionToken = true,
SendWwwAuthenticateResponseHeaders = true
};
var accessKeyHandler = new SimpleSecurityTokenHandler(
"accessKey",
AccessKey.Validate);
authentication.AddAccessKey(
accessKeyHandler,
AuthenticationOptions.ForHeader("access-key"));
authentication.AddBasicAuthentication(UserCredentials.Validate, retainPassword: true);
PassiveSessionConfiguration.ConfigureMackineKeyProtectionForSessionTokens();