Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft.Owin.Host.SystemWeb and still getting No owin.Environment item was found in the context

I've read lots of posts on this but still can't get this to work. I'm using Visual Studio 2013. I created a new MVC 5 Project and thought it would be cool to use the new facebook login integration. It works fine on my PC in IIS Express.

But when I upload it to the Production server I keep getting the very annoying "No owin.Environment item was found in the context" message.

Here's what I've done.

  1. I never changed the name of my project or assembly.
  2. The assembly name is Wodivate
  3. The namespace is Wodivate
  4. I have the default Startup.cs class which contains the following:

    using Microsoft.AspNet.Identity;
    using Microsoft.Owin;
    using Microsoft.Owin.Security.Cookies;
    using Owin;
    using System.Web.Http;
    
    [assembly: OwinStartupAttribute(typeof(Wodivate.Startup))]
    namespace Wodivate
    {
        public partial class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                ConfigureAuth(app);
            }
    
        }
    }
    
  5. In the App_Start there is a Startup.Auth.cs file which contains:

    using Microsoft.AspNet.Identity;
    using Microsoft.Owin;
    using Microsoft.Owin.Security.Cookies;
    using Owin;
    
    namespace Wodivate
    {
        public partial class Startup
        {
            // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
            public void ConfigureAuth(IAppBuilder app)
            {
                // Enable the application to use a cookie to store information for the signed in user
                app.UseCookieAuthentication(new CookieAuthenticationOptions
                {
                    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                    LoginPath = new PathString("/Account/Login")
                });
                // Use a cookie to temporarily store information about a user logging in with a third party login provider
                app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
    
                // Uncomment the following lines to enable logging in with third party login providers
                //app.UseMicrosoftAccountAuthentication(
                //    clientId: "",
                //    clientSecret: "");
    
                //app.UseTwitterAuthentication(
                //   consumerKey: "",
                //   consumerSecret: "");
    
                var facebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions()
                {
                    AppId = "xxxxxxxxxxxxxxxxxxx",
                    AppSecret = "xxxxxxxxxxxxxxxxxxxxxxxxx"
                };
                facebookOptions.Scope.Add("email"); //We want to get user's email information by adding it in scope.
                app.UseFacebookAuthentication(facebookOptions);    
    
                //app.UseGoogleAuthentication();
            }
        }
    }
    
  6. My Web.config file includes:

    <add key="owin:AppStartup" value="Wodivate.Startup, Wodivate" />
    <add key="owin:AutomaticAppStartup " value="false" />
    
  7. I also made sure to follow the post on No owin.Environment item was found in the context - only on server and installed Microsoft.Owin.Host.SystemWeb

Anyone else stuck on this? I've been hitting a wall for 3 days.

like image 206
Santy Avatar asked Apr 24 '14 17:04

Santy


People also ask

What is Owin context?

Owin is the under the hood interface between web servers and web applications. If you only write web applications in a single framework (such as ASP.NET MVC) an only run on one server platform (Windows with IIS) you can ignore Owin.

What is Owin middleware?

OWIN allows web apps to be decoupled from web servers. It defines a standard way for middleware to be used in a pipeline to handle requests and associated responses. ASP.NET Core applications and middleware can interoperate with OWIN-based applications, servers, and middleware.


2 Answers

I had this same issue and it was caused by IIS settings.

My IIS deployment had a top level site with several applications beneath it. You should ensure that the "Application Settings" of your Site level do not conflict with your application's.

In my case, I had "owin:AutomaticAppStartup = false" at the Site level, which got inherited by my application.

Summary:

  1. Open IIS Manager
  2. Select Server -> Application Settings. Ensure no owin conflicts at this level.
  3. Select Site -> Application Settings. Ensure no owin conflicts at this level.

enter image description here


Edit: I discovered you can simply add the following appSetting in web.config to override any potential inherited conflicts. I would recommend understanding if there are conflicts first though, so you can make an educated change.

<add key="owin:AutomaticAppStartup" value="true"/>
like image 56
Scott Lin Avatar answered Sep 28 '22 18:09

Scott Lin


I needed another web.config change to solve this:

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>

Though given that this setting is discouraged I tried:

<modules>
  <remove name="Owin" />
  <add name="Owin" type="Microsoft.Owin.Host.SystemWeb.OwinHttpModule, Microsoft.Owin.Host.SystemWeb" />
</modules>

but this gave me the error:

"Operation is not valid due to the current state of the object"

and I'm not sure what's happening there yet, whether it's the wrong module or something else.

like image 43
Breandán Avatar answered Sep 28 '22 19:09

Breandán