0

Is there a way to capture the response from requests served by OAuth? Specifically, I need to log the request and response from OAuthAuthorizationServerProvider.GrantResourceOwnerCredentials().

I've tried extending OwinMiddleware and overriding Invoke as shown in this post, but I'm unable to read the response body. I'd like to use a message handler as this post demonstrates, but I don't have UseHttpMessageHandler on my AppBuilder object.

Any help would be greatly appreciated. Thank you!

Update

Modifying the example provided in Brock's excellent video, here's what I need to do:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.Use(typeof(MW1));

        app.Map("/api", fooApp => {
            fooApp.Use<MW2>();
        });
    }
}

public class MW1 {
    Func<IDictionary<string, object>, Task> next;
    public MW1(Func<IDictionary<string, object>, Task> next) {
        this.next = next;
    }

    public async Task Invoke(IDictionary<string, object> env) {
        var ctx = new OwinContext(env);
        await next(env);
        // I need to be able to read: <h1>MW2 called</h1> written by MW2
        var body = ctx.Response.Body;
        // body.CanRead = False
    }
}

public class MW2 {
    Func<IDictionary<string, object>, Task> next;
    public MW2(Func<IDictionary<string, object>, Task> next) {
        this.next = next;
    }

    public async Task Invoke(IDictionary<string, object> env) {
        var ctx = new OwinContext(env);
        await ctx.Response.WriteAsync("<h1>MW2 called</h1>");
        await next(env);
    }
}

I actually need to read the response sent from the OAuth provider, but I assume it would be the same process.

Community
  • 1
  • 1
Irving
  • 1,257
  • 2
  • 16
  • 28

1 Answers1

0

Why not implement an OWIN middleware component that sits in front of the OAuth AS middleware?

Brock Allen
  • 7,385
  • 19
  • 24