I've two Web APIs:
- Identity API. It implements authentication, user registration and OAuth client registration and authentication.
- Product API. A set of RESTful APIs to work with my business.
I use the Identity API to authenticate resource owners (i.e. users with user+password credentials) to get a bearer token using OAuth2 OWIN middleware.
My issue is once I get an access token from the Identity API, I want to use it to also authorize requests to the Product API.
Both APIs are accessing the same database and both are for now in the same machine.
When I try to perform a request against the Product API I get as response message "Authorization has been denied for this request"
, while performing requests to Identity API works flawlessly.
The point is I've derived OAuthAuthorizationServerProvider
to meet some custom authentication/authorization requirements, but it never arrives to this point (ValidateClientAuthentication
and GrantResourceOwnerCredentials
methods are never called) when issuing requests to the Product API.
I've already discarded that the order of OWIN middleware may be affecting the authentication flow: both APIs are configured in the same exact way.
Some days ago...
... before trying to work this way, I was thinking about creating both a custom OAuthAuthorizationServerProvider
and ASP.NET Identity user store to actually query the Identity API internally, thus, both authentication and authorization would be verified in the OWIN app which issued the access token.
I've already implemented a custom user store to ASP.NET Identity (GitHub repository), and I've not already implemented a OAuthAuthorizationServerProvider
to issue HTTP requests instead of using the underlying database directly too.
Anwyay, I would like to know if I can avoid going this way for a while, and if I can issue access tokens from an OWIN app and consume access tokens from a different OWIN app with also a different host and port.
Update: Debugging System.Web.Http
I've download System.Web.Http
source code from ASP.NET Web Stack GitHub repository and I've also compiled it, and I've linked the compiled assembly to my Product API WebAPI project to debug AuthorizeAttribute
.
The whole bearer token is received but actionContext.ControllerContext.RequestContext.Principal
is null
. I suspect that some OAuth-related middleware isn't decrypting and assigning the user to the whole property.
The point is the same token will work on the Identity API.
Check the following screenshot where I can demonstrate the bearer token is being received:
Update 2: Identity API can authorize requests using the emitted access token...
I can confirm that the access token works to authorize requests to Identity API's resources (for example, I've implemented an UserController
to let the Identity API register and manage users, and most controller actions are marked with [Authorize]
attribute...).