2014-11-27 3 views
4

, 나는 다음과 같은 코드가 액세스 제어 - 허용 - 헤더 목록에 존재하지 않았다. 나도 같은 API에 있지만 다른 브라우저, 인터넷 익스플로러 11에서 동일한 컴퓨터에서 연결할 때, 나는 다음과 같은 오류 얻을 :요청 헤더 내 API에서

SEC7123: Request header x-api-applicationid was not present in the Access-Control-Allow-Headers list.

을 나는 코드를 디버깅, 나는 헤더가 추가됩니다 참조 응답.

IE11 response

IE는 무엇을 기대 하시는가 : 심지어 IE는 헤더를 보여줍니다?

업데이트

나는에

new[] { 
    "access-control-allow-origin", 
    "accept", 
    "x-api-applicationid", 
    "content-type", 
    "authorization" 
} 

에서 헤더의 순서를 변경하는 경우 :

SEC7123: Request header access-control-allow-origin was not present in the Access-Control-Allow-Headers list.

:

new[] { 
    "content-type", 
    "accept", 
    "access-control-allow-origin", 
    "x-api-applicationid", 
    "authorization" 
} 

오류 메시지가 변경

그래서 세 번째 헤더에서 항상 오류가 발생합니다.

답변

0

나는 here 코드 조각을 찾았습니다.

//Startup.cs 
public void ConfigureOAuth(IAppBuilder app) 
{ 
    app.Use(async (context, next) => 
    { 
     IOwinRequest req = context.Request; 
     IOwinResponse res = context.Response; 
     if (req.Path.StartsWithSegments(new PathString("/oauth2/token"))) 
     { 
      var origin = req.Headers.Get("Origin"); 
      if (!string.IsNullOrEmpty(origin)) 
      { 
       res.Headers.Set("Access-Control-Allow-Origin", origin); 
      } 
      if (req.Method == "OPTIONS") 
      { 
       res.StatusCode = 200; 
       res.Headers.AppendCommaSeparatedValues("Access-Control-Allow-Methods", "GET", "POST"); 
       res.Headers.AppendCommaSeparatedValues("Access-Control-Allow-Headers", "authorization", "content-type", "x-api-applicationid", "access-control-allow-origin"); 
       return; 
      } 
     } 
     await next(); 
    }); 

    // rest of owin Oauth config 
} 

나는

2

그것은 당신의 AJAX에서 Content-Type 헤더의 철자를 잘못 쓴만큼 간단하지 있는지 확인 내 CustomOAuthProvider.cs에서 MatchEndpoint 방법을 제거했습니다. 나는 프리 플라이트를 필요로하지 않는 application/x-www-form-urlencoded 콘텐츠 형식과 프리 플라이트 옵션이를 얻고 있었다, 그러나 나는 내 contentType 옵션으로

content-type: application/x-www-form-urlencoded

대신

application/x-www-form-urlencoded

했다 .

잘못된 :

$.ajax({ 
    url: 'http://www.example.com/api/Account/Token', 
    contentType: 'content-type: application/x-www-form-urlencoded', 
    method: 'POST', 
    data: { 
     grant_type: "password", 
     username: $('#username').val(), 
     password: $('#password').val() 
    }, 
}); 

RIGHT :

$.ajax({ 
    url: 'http://www.example.com/api/Account/Token', 
    contentType: 'application/x-www-form-urlencoded', 
    method: 'POST', 
    data: { 
     grant_type: "password", 
     username: $('#username').val(), 
     password: $('#password').val() 
    }, 
}); 
0

필요가 대신 단지 액세스 -에서 첫 번째 배열 요소로 쉼표로 분리 된 값을 추가 배열 요소를 추가하는 MatchEndPoint

를 제거하기 제어 허용 헤더

대신

context.OwinContext.Response.Headers.Add("Access-Control-Allow-Headers", 
       new[] { 
        "access-control-allow-origin", 
        "accept", 
        "x-api-applicationid", 
        "content-type", 
        "authorization" 
       }); 

사용

context.OwinContext.Response.Headers.Add("Access-Control-Allow-Headers", 
    new[] { 
     "access-control-allow-origin,accept,x-api-applicationid,content-type,authorization" 
    });