ASP.Net WebAPI OWIN: Why would Request.GetOwinContext() return null?

asp.net-web-api, owin

Solution

Another cause (especially after upgrading form ASP.NET MVC4 and / or Empty WebApi Template) is missing Startup.cs file in the root of WebAPI project.

Also, make sure that you have installed Microsoft.Owin.Host.SystemWeb package.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(TestMVC5.Startup))]

namespace TestMVC5
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}

Problem

In my production code we're having a problem where Request.GetOwinContext() always returns null. I setup a small test WebAPI controller to try and isolate the problem: ``` public class TestController : ApiController { [HttpGet] public async Task<IHttpActionResult> GetAsyncContext(string provider) { if (HttpContext.Current.GetOwinContext() == null) return this.BadRequest("No HttpContext.Current Owin Context"); if (Request.GetOwinContext() == null) return this.BadRequest("No Owin Context"); return this.Ok(); } [HttpGet] public IHttpActionResult GetContext(string provider) { if (HttpContext.Current.GetOwinContext() == null) return this.BadRequest("No HttpContext.Current Owin Context"); if (Request.GetOwinContext() == null) return this.BadRequest("No Owin Context"); return this.Ok(); } } ``` At first I thought it might have something to do with the action method running asynchronously, but after running the above, it turns out that in both versions, Request.GetOwinContext() returns null. I am using Microsoft.AspNet.WebApi.Owin.5.1.1 (which is where it seems the GetOwinContext() extension method is defined). Any ideas on what's happening here???

Original source