0

I'm having a weird issue regarding Owin and its context. I have a controller action that looks like this

[HttpPost]
public ActionResult Login(string username, string password)
{
    var ctx = HttpContext.GetOwinContext();
    // code omitted
    return new HttpUnauthorizedResult();
}

If I set a breakpoint at var ctx and debug my application the variable gets its value. But if just start the applicaiton normally and run it with IIS Express (ctrl+f5) the application breaks at var ctx and gives me the error.

"No owin.Environment item was found in the context."

What's causing this error?

I'm running this in Visual Studio 2013, .Net version 4.5.1. Thanks for your help!

Please let me know if you think you need more code from startup.cs or anywhere else.

EDIT: This is my startup conf.

[assembly: OwinStartup(typeof(XXX.Startup))]
namespace XXX
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}

namespace XXX
{
    public partial class Startup
    {
        public void ConfigureAuth(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = AuthenticationTypes.Cookie,
                AuthenticationMode = AuthenticationMode.Active,
                SlidingExpiration = true,
                ExpireTimeSpan = new TimeSpan(0, 30, 0),
            });

            app.UseMyAuthentication(new MyAuthenticationOptions()
            {
                AuthenticationType = AuthenticationTypes.MyAuthType,
                SignInAsAuthenticationType = AuthenticationTypes.Cookie,
                AuthenticationMode = AuthenticationMode.Passive,
                CallbackPath = new PathString("/authorization/callback")
            });

        }
    }
}
Andreas
  • 2,336
  • 2
  • 28
  • 45
  • Look at [this](http://stackoverflow.com/questions/18232549/no-owin-environment-item-was-found-in-the-context) thread. – Yuval Itzchakov May 29 '15 at 09:38
  • Added some configuration. @YuvalItzchakov, It finds the startup class, but when getting the context when not doing ctrl+f5 it throws the exception, but not when doing f5 and debugging through the getOwinContext method. – Andreas Jun 01 '15 at 06:22

1 Answers1

0

Looks that you can't "register"* the startup class more than once, removing [assembly: OwinStartup(typeof(XXX.Startup))] made it register the startup class when not debugging. Still don't understand why it worked when debugging though.

*By following the convention of placing the StartUp class directly under the assembly the owin reflection code finds the StartUp class.

Andreas
  • 2,336
  • 2
  • 28
  • 45