2016-03-31 8 views
1

IndentityServerとOwinの自己ホスト型Web APIを使用してOAuthを実装しました。クライアントが認証されたエンドポイントにアクセスしようとするたびに、Microsoft.Owin.Security.OpenIdConnectミドルウェアを使用して、認証のためにIndentityServerへの呼び出しを傍受してリダイレクトします。 API呼び出しの場合、302を返すのではなく、IdentityServerのURLを持つ場所ヘッダーを持つ401を返します。Microsoft.Owin.Security.OpenIdConnectミドルウェアからの応答時にロケーションヘッダーを追加

AuthenticationMode = AuthenticationMode.Passive 

OWINミドルウェアを取得することはできますが、ロケーションヘッダーを追加することはできません。私たちはこれをどのように達成しますか?ヘッダーを設定しようとしましたが(下のコードを参照)、動作しません。応答はミドルウェアによって内部的に構築されているようです。

appBuilder.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions 
      { 
       Authority = idSrvUri.ToString(), 
       AuthenticationType = WebServiceConstants.OAuthAuthType, 
       ClientId = "beocloud", 
       Scope = "openid profile roles", 
       ResponseType = "id_token token", 
       AuthenticationMode = AuthenticationMode.Passive, 
       SignInAsAuthenticationType = WebServiceConstants.OAuthAuthType, 
       UseTokenLifetime = false, 


       Notifications = new OpenIdConnectAuthenticationNotifications 
       { 
        RedirectToIdentityProvider = n => 
        { 
         if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.AuthenticationRequest) 
         { 
          n.ProtocolMessage.RedirectUri = n.Request.Scheme + "://" + n.Request.Host + "/"; 
          n.Response.Headers.Add("Location", new []{n.ProtocolMessage.CreateAuthenticationRequestUrl()}); 
         } 

         if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest) 
         { 
          var idTokenHint = n.OwinContext.Authentication.User.FindFirst("id_token"); 

          if (idTokenHint != null) 
          { 
           n.ProtocolMessage.IdTokenHint = idTokenHint.Value; 

          } 
         } 

         return Task.FromResult(0); 
        } 
       } 
      }); 
     } 

答えて

0

これが問題あなたのMVCのコードが側面に沿ったWeb APIをホストしている - あなたはそれのAPI側の間違ったセキュリティミドルウェアを使用しています。 OIDCミドルウェアは、HTTPコールはリダイレクトできるブラウザウィンドウからのものであることを前提としています。

APIを別のホストとパイプラインに分割し、トークンベースのセキュリティアーキテクチャとミドルウェアを使用することをお勧めします。 github repoにはこのパターンのサンプルがたくさんあります。

関連する問題