title: Resolve No owin.Environment item was found in the context
body: <p>I have found many threads discussing this problem but getting any of them to work has eluded me so far.</p>
  
  <p>I have been trying to follow <a href="https://prodinner.codeplex.com/" rel="nofollow">ProDinner</a> as a way of learning how to implement role based authentication in my app. But I cannot work out what I am missing. Every time I try to log in I get </p>
  
  <blockquote>
    <p>No owin.Environment item was found in the context</p>
  </blockquote>
  
  <p>After a lot of debugging and searching I have found the <code>Startup</code> class is not being called. I've tried to resolve by stepping through the proDinner code to see, how <code>Startup</code> is being called and googled (a lot!). My searches lead me to adding (although neither exist in the proDinner project)</p>
  
  <pre><code>&lt;add key="owin:AutomaticAppStartup" value="Tjs.Startup, Tjs" /&gt;
  </code></pre>
  
  <p>then </p>
  
  <pre><code>&lt;add key="owin:AutomaticAppStartup" value="Tjs.Web.Admin.Startup, Tjs" /&gt;
  </code></pre>
  
  <p>Below I have added all of what I believe is the relevant code, but please let me know if I am missing anything.</p>
  
  <p>The AccountController is a copy paste of ProDinner, with the addition of a parameterless constructor, I was unable to work out how that parameter is being passed in the Prodinner project (could that be the problem?)</p>
  
  <p><strong>Tjs</strong>: solution name</p>
  
  <p><strong>Tjs.Web.Admin</strong>: namespace</p>
  
  <p><strong>Owin packages</strong></p>
  
  <pre><code>&lt;package id="Microsoft.Owin" version="3.0.1" targetFramework="net45" /&gt;
  &lt;package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net45" /&gt;
  &lt;package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net45" /&gt;
  &lt;package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net45" /&gt;
  &lt;package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net45" /&gt;
  </code></pre>
  
  <p><strong>root of project</strong></p>
  
  <pre><code>using Microsoft.Owin;
  using Owin;
  
  using Tjs.Web.Admin;
  using Tjs.Web.Admin.App_Start;
  
  [assembly: OwinStartup(typeof(Startup))]
  namespace Tjs.Web.Admin
  {
    public class Startup
    {
      public void Configuration(IAppBuilder app)
      {
          OwinConfig.ConfigureAuth(app);
      }
    }
  }
  </code></pre>
  
  <p><strong>App_Start</strong></p>
  
  <pre><code>using Microsoft.AspNet.Identity;
  using Microsoft.Owin;
  using Microsoft.Owin.Security.Cookies;
  using Owin;
  namespace Tjs.Web.Admin.App_Start
  {
    public static class OwinConfig
    {
      public static void ConfigureAuth(IAppBuilder app)
      {
          app.UseCookieAuthentication(new CookieAuthenticationOptions
          {
              AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
              LoginPath = new PathString("/Account/SignIn")
          });
      }
    }
  }
  </code></pre>
  
  <p><strong>AccountController</strong></p>
  
  <pre><code>    private readonly IUserService userService;
      public AccountController()
      {
  
      }
  
      public AccountController(IUserService userService)
      {
          this.userService = userService;
      }
  
      private IAuthenticationManager Authentication
      {
          get
          {
              return HttpContext.GetOwinContext().Authentication;
          }
      }
  
      private void SignInOwin(string name, bool rememberMe, string role)
      {
          var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, name) },
              DefaultAuthenticationTypes.ApplicationCookie,
                  ClaimTypes.Name, ClaimTypes.Role);
  
          //foreach (var role in roles)
          //{
          //    identity.AddClaim(new Claim(ClaimTypes.Role, role));
          //}
          identity.AddClaim(new Claim(ClaimTypes.Role, role));
  
          Authentication.SignIn(new AuthenticationProperties
          {
              IsPersistent = rememberMe
          }, identity);
      }
  
      public ActionResult SignIn()
      {
          return View(new SignIn { Email = "o", Password = "1" });
      }
  
      [HttpPost]
      public ActionResult SignIn(SignIn input)
      {
          if (!ModelState.IsValid)
          {
              input.Password = null;
              input.Email = null;
              return View(input);
          }
  
          Staff staff;
  
          //ACHTUNG: remove this in a real app
          if (input.Email == "o" &amp;&amp; input.Password == "1")
          {
              staff = new Staff { Email = "o", Role = new Role { Description = "admin" } };
          }
          else
          {
              staff = userService.Get(input.Email, input.Password);
          }
  
          if (staff == null)
          {
              ModelState.AddModelError("", "Try Login: o and Password: 1");
              return View();
          }
  
          SignInOwin(staff.Email, input.Remember, staff.Role.Description);
  
  
          return RedirectToAction("index", "home");
      }
  
      public ActionResult SignOff()
      {
          Authentication.SignOut();
          return RedirectToAction("SignIn", "Account");
      }
  }
  </code></pre>
  
  <p>it fails at</p>
  
  <pre><code>private IAuthenticationManager Authentication
  {
      get
      {
          return HttpContext.GetOwinContext().Authentication;
      }
  }
  </code></pre>