2016-10-31 17 views
0

2つのプロジェクトがあり、1つはMVC(角度を使用)、もう1つはWebAPIです。私はこのpostが、読みAngularから呼び出されたときにWebAPIでWindows認証が機能しない

HTTP Error 401.2 - Unauthorized You are not authorized to view this page due to invalid authentication headers.

Most likely causes:

  • No authentication protocol (including anonymous)is selected in IIS.
  • Only integrated authentication is enabled, and a client browser was used that does not support integrated authentication.
  • Integrated authentication is enabled and the request was sent through a proxy that changed the authentication headers before they reach the Web server.
  • The Web server is not configured for anonymous access and a required authorization header was not received.
  • The "configuration/system.webServer/authorization" configuration section may be explicitly denying the user access.

:Windows認証は、私はAJAXが、私はエラーを次取得WebAPIのに角度によってMVCサイトから呼び出しを作っていたときに、しかし

MVCで(このarticleのおかげで)正常に動作しています私はJQueryやAngularを使ってHttpClientを呼び出しています。

ご注意:ブラウザからWebAPI URLにヒットした場合、認証が正常に機能します。だからAJAXのリクエストとは何か関係があります。

これは、Global.asaxの

protected void Application_BeginRequest() 
{ 
    if (ValidateRequest()) 
    { 
     //var origin = Request.Headers["Origin"]; 
     Response.Headers.Remove("Access-Control-Allow-Origin"); 

     Response.AddHeader("Access-Control-Allow-Origin", matchedOrigin); 

     Response.Headers.Remove("Access-Control-Allow-Headers"); 
     Response.AddHeader("Access-Control-Allow-Headers", CustomConfig.HEADERS); 

     Response.Headers.Remove("Access-Control-Allow-Methods"); 
     Response.AddHeader("Access-Control-Allow-Methods", CustomConfig.METHODS); 
    } 

    // This is to avoid "Method 405 Not allowed" error 
    if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS") 
    { 
     Response.Flush(); 
     Response.End(); //Send the Empty Response for Options (Preflight Request) 
    } 
} 

で私のコードで私は十分に研究を行っているが、解決策を見つけることができませんでした。だからカップル。

  • どのように私は上記の問題を解決することができます
  • 第二に、私のシナリオ(そして、プロジェクトの設定)に基づいて、Windows認証を使用するための最良の方法は何ですか。

答えて

0

JavaScriptが関与しているときにブラウザで直接動作するのであれば、それはCORSの問題になりますので、飛行前のOPTIONS動詞の処理を含め、すべて有効にしてください。

あなたの場合は、資格情報を渡していることも確認する必要があります。

[EnableCors(origins: "http://myclient.azurewebsites.net", headers: "*", 
    methods: "*", SupportsCredentials = true)] 

資格情報では、ワイルドカードを使用できるとは思わないので、明示的なリストが必要です。

このリンクを参照してください:https://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api#credentials

角度$httpリクエストでは、プロパティがwithCredentials: trueであることを確認してください。

それとも、その後はJQuery AJAX呼び出しを行っている場合:

public class ExampleMessageHandler : DelegatingHandler 
    { 
     protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, 
      CancellationToken cancellationToken) 
     { 

      if (request.Headers.Contains("Origin") && request.Method.Method == "OPTIONS") 
      { 
       var response = new HttpResponseMessage(); 
       response.StatusCode = HttpStatusCode.OK; 
       response.Headers.Add("Access-Control-Allow-Origin", "http://localhost:8080/"); 
       response.Headers.Add("Access-Control-Allow-Credentials", "true"); 
       response.Headers.Add("Access-Control-Allow-Headers", "Origin, Content-Type, Accept, Authorization"); 
       response.Headers.Add("Access-Control-Allow-Methods", "DELETE, POST, PUT, OPTIONS, GET"); 
      } 
      return base.SendAsync(request, cancellationToken); 
     } 
    } 

xhrFields: { 
     withCredentials: true 
    } 

あなたはまだ飛行前の問題は、この(原点(複数可)を変更する)のようなカスタムメッセージハンドラを試みてもらって下さい

その後:私はWebAPIのP上のWindows認証を有効にする前に、それは完璧に働いていたので、それがCORSの問題です疑う

public static void Register(HttpConfiguration config) 
    { 
     config.MessageHandlers.Add(new ExampleMessageHandler()); 
+0

ルーツ。私は信任状で何かをしなければならないと信じていますが、何が分からないのでしょう。 – programmerboy

+0

そして、あなたはSupportsCredentialsを本当に持っています... –

+0

追加の案内のために私の編集を参照してください –

関連する問題