Authenticating requests to Microsoft.Owin.Testing.

2020-08-10 07:49发布

问题:

I'm using the owin TestServer class to unit test my web api. It's working great except that I'm unsure how to authenticate requests, so I can't really test endpoints that require authentication, other than to make sure that unauthenticated requests are unauthorized.

I am configuring WebApi to use only bearer token authentication as indicated here:

// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

Any ideas how I could do this easily?

回答1:

I found useful documentation to use TestServer+OAuth authentication
http://www.aaron-powell.com/posts/2014-01-12-integration-testing-katana-with-auth/

He also provides whole VS solution at GitHub.
https://github.com/aaronpowell/Owin.AuthenticatedTests/tree/a154931dd82e5180daf7163c2129f3a90401df55