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.
- I never changed the name of my project or assembly.
- The assembly name is Wodivate
- The namespace is Wodivate
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);
}
}
}
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();
}
}
}
My Web.config file includes:
<add key="owin:AppStartup" value="Wodivate.Startup, Wodivate" />
<add key="owin:AutomaticAppStartup " value="false" />
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.
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:
- Open IIS Manager
- Select Server -> Application Settings. Ensure no owin conflicts at this level.
- Select Site -> Application Settings. Ensure no owin conflicts at this level.
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"/>
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.
Did you try to do something like this?
<system.webServer>
<modules runAllManagedModulesForAllRequests="false" />
<handlers>
<remove name="StaticFile" />
<add name="OWIN" path="*" verb="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler" />
</handlers>
</system.webServer>
Have you checked mode of app pool being used. OWIN works with integrated mode of app pool. It wont work in clasic mode.
When you get the exception 'No owin.Environment item was found in the context.' from Request.GetOwinContext(), it usually means that the OWIN startup class detection failed.
Make sure IIS is executing the code in Startup.cs (in the root folder) on startup. To check if it does on a production server, I would recommend the following. 1) Insert the following line:
...
System.Diagnostics.Trace.WriteLine("OwinStartup " + this.GetType().Namespace);
ConfigureAuth(app);
...
2) Get the DebugView utility from here: https://technet.microsoft.com/en-us/library/bb896647.aspx
3) Execute DbgView.exe as administrator and make sure the option "Capture / Capture Global Win32" IS enabled.
4) You should see a trace message from the above code when IIS launches (make sure it's actually restarted by touching web.config).
5) If it's not executed, check here: http://www.asp.net/aspnet/overview/owin-and-katana/owin-startup-class-detection
There is a number of reasons that it's not executed. Many of them are on stackoverflow. One more that I'll add is this one: Make sure you don't have the setting "batch=false" on the 'compilation' node of web.config.
What happens is that your BIN is not including the reference. In the preferences change it to "Copy Local" to TRUE. then publish it again.
I struggled with this for DAYS!! Tried everything in every post I could find and this fixed the issue. I was developing in VS 2013 and publishing to a Server 2008 VM and I kept getting the same "No owin.Environment item was found in the context" message.
I put this entry in web config for modules and it fixed it. I am not sure I understand why it fixed it but it did.
<modules runAllManagedModulesForAllRequests="true" />