0

I have created a token .NET web API endpoint to get an OAuth bearer token. After retriving the token I call another endpoint of my API and I pass in the authorize header my bearer token but if I check the principal in the RequestContext it is always null. Why my owin server doesn't create a principal object.

Is it right to register my authorization provider like this:

 builder.RegisterType<SimpleAuthorizationServerProvider>()
            .As<IOAuthAuthorizationServerProvider>()
            .PropertiesAutowired()
            .SingleInstance(); 

Or should it be .InstancePerRequest(). But if I use the instance per request I receive an error. Has anyone a working example for this use case. I am very desperately because I have tried multiple example but without resolution. This is even my second thread (Token Based Authentication using ASP.NET Web API 2 and Owin throws 401 unautorized) on this problem but I think my problem resides on autofac because it's the only difference to other working samples.

Community
  • 1
  • 1
cpiock
  • 1,275
  • 2
  • 17
  • 44

1 Answers1

0

This answer solves my problem https://stackoverflow.com/a/36769653/5441093

Change in the GrantResourceOwnerCredentials method this to resolve my userbl class:

var autofacLifetimeScope = OwinContextExtensions.GetAutofacLifetimeScope(context.OwinContext);
        var userBl = autofacLifetimeScope.Resolve<IUserBl>();

instead of using the injection of autofac

Community
  • 1
  • 1
cpiock
  • 1,275
  • 2
  • 17
  • 44