38

Microsoft recently introduced new ASP.NET Identity - replacement for old (Simple)Membership. Unfortunately, I can't use this new membership system in my old project because it throws System.InvalidOperationException: No owin.Environment item was found in the context. This is a known bug, but Microsoft keeps silence about this issue. The easiest way to reproduce this bug - it's to create a new web application (MVC, WebForms or WebApi - doesn't matter) in VS 2013 (with Web Tools 2013 Preview Refresh) and then go to the login page. It will work. Then change namespace in your application to anything else than original namespace and login page will throw that error. Changing namespace back to original (the one you used at the creation of a project) will solve this problem.

It looks like .net stores somewhere something related to the original namespace, but I can't find what and where, it's not in the project folder. I know that stackoverflow is not a place for a bug reports, I just hoping that someone already found a solution for this issue or maybe people involved in the development of ASP.NET Identity will see this.

graycrow
  • 3,675
  • 6
  • 26
  • 28
  • 1
    For me [this](https://stackoverflow.com/a/22650502/8321861) fixed it for me - no credits to me. – Meikel Mar 13 '18 at 14:33
  • Note that OwinContext is not accessible from Global.asax See also https://stackoverflow.com/q/24660380/18192 – Brian Jan 09 '19 at 19:30

14 Answers14

28

Most likely it cannot find the OWIN Startup class. The default convention for the Startup class is [AssemblyName].Startup. If you're no longer following that convention you'll need to specify the full name of your Startup class in the Web.Config.

The next release of Microsoft.Owin.Host.SystemWeb package now throws detailed exception messages when the Startup class cannot be found.

pranav rastogi
  • 4,124
  • 23
  • 23
19

I had the same issue, it was fixed after making sure this line was in web.config:

<add key="owin:AutomaticAppStartup" value="true" />
cesar-moya
  • 581
  • 5
  • 8
19

I had the exact same error, but as it turned out I had another configuration problem in my web.config. My web.config was missing the attribute defaultLanguage="c#" in the compilation element under system.web.

In this case it will default to VB. So unless you have your Startup class written in VB you should change the default language to C#.

Not correct:

<compilation debug="true" optimizeCompilations="true" targetFramework="4.6.1">

This is correct (unless you use VB):

<compilation debug="true" defaultLanguage="c#" optimizeCompilations="true" targetFramework="4.6.1">
Drol
  • 189
  • 1
  • 6
11

Cleaning ASP.NET temporary files helped me with this exact problem

Gatis Bergšpics
  • 441
  • 6
  • 14
  • Deleting the contents of this specific folder worked for me: c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files – Vito May 19 '20 at 14:18
  • I also saw this issue on one of our dev machines and this seemed to fix it at that time. There must be many reasons why this error happens, hence the many solutions. – Michael Adamission Oct 13 '20 at 15:27
8

I created two new projects called TesteMvc5.2 and TesteMvc5.0 and both of them didn't work at start

this is because the default namespace is different from the assembly name. but after I put the line

<add key="owin:AppStartup" value="TesteMvc5._2.Startup, TesteMvc5.2" />

on the web.config it worked fine.

Luiz Bicalho
  • 813
  • 1
  • 12
  • 30
4

If you happened to have copied the below config from MVC4, you should remove it from web.config

<add key="owin:AutomaticAppStartup" value="false" />
RobH
  • 3,604
  • 1
  • 23
  • 46
4

I tried everything mentioned on this page but nothing worked. Then I found out about a setting in IIS named owin:AutomaticAppStartup. You can find it in the Application Settings page of the IIS Manager for the Default Web Site. Check to see if that setting is true. If not set to true. This worked for me.

This is the website where I found the answer: http://gotoanswer.stanford.edu/?q=Microsoft.Owin.Host.SystemWeb+and+still+getting+No+owin.Environment+item+was+found+in+the+context

Ron Dow
  • 41
  • 1
  • 3
    So there are actually two steps: 1 add suggested by Luiz and 2. – Arvind Singh Mar 26 '15 at 11:03
  • Arvind, could you please post the more of your web.config to show how this works? Thanks! – curious1 Apr 20 '15 at 20:15
  • 1
    @Ron, that link is broken – KevinDeus Aug 31 '16 at 21:53
  • @ArvindSingh - your solution works perfect. We have a sub application hosted as part of the main website and while we had to do the usual "inheritinchildapplications = false" the child application used to fail (not all the time) with this error So for instance it worked 50% of the time and 50% of the time it complained about OWIN. This application was untouched for about 3 years. While it worked fine locally on the server with the other solutions, it won't when you're accessing it off a domain name, hence these 2 statements fit in perfectly in conjunction. – Preetham Apr 19 '20 at 10:57
3

I had this same issue. I fixed it with the web.config.

However I had changed the assembly name and namespace and did not find the original assembly name anywhere anymore.

I then discovered that clean was not removing the original assembly from the bin.

Aftter deleting the bin litter, I was able to remove the web.config OWIN entry.

user2697956
  • 119
  • 1
  • 2
  • clean the bin and obj directory did the trick for me as well (combined with the web.config alteration). – markwilde Jan 06 '14 at 21:35
3

None of the above answers worked for me.

Turned out my project was missing the "Startup" class that contains the following:

using Microsoft.Owin;
using Owin;

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

This file called "Startup.cs" is located on the root folder (~/) of your project.

My "Web.config" doesn't have any of this "Owin" configuration posted on the other replies.

Daniel
  • 1,089
  • 11
  • 24
1

Had same problem. Thanks for the shared solutions. this..

<add key="owin.AppStartup" value="Namespace.Startup, Namespace"/>
<add key="owin:AutomaticAppStartup" value="false"/>  

fixed for me

Zia UsafXai
  • 73
  • 1
  • 10
1

I have no idea why this works but it did!

My problem was in VS2013. In the WebConfig, debug was set to true and I got that error. When I set it to false it worked fine and then I reset to true and it continued to work OK!

At first when debug was true before changing to false, I put a break point in my StartUp code and it wasn't reached at all. After changing to false pressing save and then back to true the StartUp code was called and the program works like it should.

Dov Miller
  • 1,958
  • 5
  • 34
  • 46
0

I experienced this error in an Optimizely (Episerver) solution where I had two feature branches using the same CMS database. In one feature branch I was working on a proof of concept using a visitor criterion. So I had created something like this:

    public class SomeVisitorCriterionSettings : CriterionModelBase
    {
        public override ICriterionModel Copy()
        {
            return base.ShallowCopy();
        }
    }



[VisitorGroupCriterion(
    Category = "Some category",
    DisplayName = "My visitor criterion")]
public class SomeVisitorCriterion : CriterionBase<SomeVisitorCriterionSettings>
{
    public override bool IsMatch(IPrincipal principal, HttpContextBase httpContext)
    {
        // match logic here..
    }
}

And within Episerver -> CMS -> Visitor Groups I had created an instance of this visitor criterion. After switching to the other branch where this code did not exist, but the instance in the database did, the Owin exception was thrown.

Deleting the visitor criterion in the CMS resolved the issue, but I honestly have no idea why this sort of exception is thrown. I would love to know though..

Frostrar
  • 337
  • 6
  • 18
0

I have tried all of the above suggestions, without success; then reading the documentation at: https://learn.microsoft.com/en-us/aspnet/aspnet/overview/owin-and-katana/owin-startup-class-detection

I understood that the error was in the call to the assembly in the Startup.cs file:

wrong code:

[assembly: OwinStartupAttribute(typeof([AssemblyName].Startup))]

right code:

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

so, I fixed the error removing the word Attribute from OwinStartupAttribute

elena
  • 123
  • 1
  • 11
-1

adding default language to compilation in web.config did it for me!

David
  • 1