I'm building an Web API 2 project with Bearer Token Authentication.
The request for the access_token is working but not my other methods. API is returning the following:
No OWIN authentication manager is associated with the request
Full Response Message
{
"Message":"An error has occurred.",
"ExceptionMessage":"No OWIN authentication manager is associated with the request.",
"ExceptionType":"System.InvalidOperationException",
"StackTrace":" at System.Web.Http.Owin.PassiveAuthenticationMessageHandler.SuppressDefaultAuthenticationChallenges(HttpRequestMessage request)\r\n at System.Web.Http.Owin.PassiveAuthenticationMessageHandler.<SendAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.HttpServer.<SendAsync>d__0.MoveNext()"
}
Startup.cs
public partial class Startup
{
public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
static string PublicClientKey = "XXX";
public void ConfigureAuth(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientKey),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
app.UseOAuthBearerTokens(OAuthOptions);
}
}
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ControllerAndAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Global.asax
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
I searched for this error and found some people who said that it was resolved by the following:
- Webconfig: Set
<modules runAllManagedModulesForAllRequests="true">
- Installing Microsoft.Owin.Host.SystemWeb
- Check if I was using
Context.GetOwinContext()
instead ofRequest.GetOwinContext()
Webconfig options didn't work.
I had the Host.SystemWeb package.
And I didn't call GetOwinContext anywhere.
Any idea?
Thank you.