11

トークン エンドポイント レスポンスからレスポンス ボディを変更したいと考えています。

/Tokenリクエストを MessageHandler でインターセプトしようとしましたが、うまくいきません。

メソッドをオーバーライドすることで応答に追加情報を追加できOAuthAuthorizationServerProvider.TokenEndpointますが、独自の応答本文を作成することはできません。

/Tokenリクエストを傍受する方法はありますか?


編集

次のように、トークンエンドポイントの応答から応答本文のコンテンツを削除する方法を見つけました。HttpContext.Current.Response.SuppressContent = true;

私の目標を達成するための正しい方法のようですが、context.AdditionalResponseParameters.Add()メソッドを使用してカスタム情報を追加すると、SuppressContent変更がブロックされます。

今、私はこのようなものを持っています:

// Removing the body from the token endpoint response
HttpContext.Current.Response.SuppressContent = true;
// Add custom informations
context.AdditionalResponseParameters.Add("a", "test");
4

4 に答える 4

6

JSON トークン応答に新しい項目を単純に追加するには、通知TokenEndpointResponseの代わりに使用できます。TokenEndpoint


OAuth2 認証サーバーによって準備されたトークン応答を独自のものに完全に置き換える方法を探している場合、通知を呼び出した後にプロパティをOAuthAuthorizationServerHandler.InvokeTokenEndpointAsyncチェックしないため、悲しいことにそれを行う簡単な方法はありません。OAuthTokenEndpointContext.IsRequestCompletedTokenEndpointResponse

https://github.com/aspnet/AspNetKatana/blob/dev/src/Microsoft.Owin.Security.OAuth/OAuthAuthorizationServerHandler.cs

これは既知の問題ですが、修正を提案したときに Katana 3 に含めるには遅すぎました。

試してみてください: Katana 3.0 および 4.0 用に設計されOwin.Security.OpenIdConnect.Serverた のフォークです。OAuthAuthorizationServerMiddleware

https://www.nuget.org/packages/Owin.Security.OpenIdConnect.Server/1.0.2

もちろん、デフォルトのトークン要求処理をバイパスできるようにするための正しいチェックが含まれています (これは、フォークしたときに最初に修正したものの 1 つでもありました)。

于 2015-02-23T21:38:22.727 に答える
-2

要求と応答をインターセプトする最善の方法は、要求がパイプラインの IControllerFactory ハンドラーに到達した後にそれを回避したい場合は、MessageHandler を使用することです。その場合は明らかにカスタムの「属性」を使用します。

過去に MessageHandlers を使用して、API/トークンへのリクエストをインターセプトし、新しいリクエストを作成してレスポンスを取得し、新しいレスポンスを作成しました。

    protected override async Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request, CancellationToken cancellationToken)
    {
        //create a new auth request
        var authrequest = new HttpRequestMessage();
        authrequest.RequestUri = new Uri(string.Format("{0}{1}", customBaseUriFromConfig, yourApiTokenPathFromConfig));

        //copy headers from the request into the new authrequest
        foreach(var header in request.Headers)
        {
            authrequest.Headers.Add(header.Key, header.Value);
        }

        //add authorization header for your SPA application's client and secret verification
        //this to avoid adding client id and secret in your SPA
        var authorizationHeader =
            Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}", _clientIdFromConfig, _secretKeyFromConfig)));

        //copy content from original request
        authrequest.Content = request.Content;

        //add the authorization header to the client for api token
        var client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(request.Headers.Authorization.Scheme, authorizationHeader);
        var response = await client.PostAsync(authrequest.RequestUri, authrequest.Content, cancellationToken);

        if(response.StatusCode == HttpStatusCode.OK)
        {
            response.Headers.Add("MyCustomHeader", "Value");
            //modify other attributes on the response
        }

        return response;
    }

これは私にとって完璧に機能します。ただし、WebApiConfig.cs ファイル (ASP.NET MVC を使用している場合は RouteConfig.cs) に必要なこのハンドラーの構成があります。

ハンドラーで何がうまくいかないのか詳しく説明していただけますか?

于 2015-07-11T10:01:04.153 に答える