【问题标题】:Microsoft.Owin.Host.SystemWeb and still getting No owin.Environment item was found in the contextMicrosoft.Owin.Host.SystemWeb 并且仍然在上下文中找到 No owin.Environment 项
【发布时间】:2014-04-24 17:44:46
【问题描述】:

我已经阅读了很多关于此的帖子,但仍然无法使其正常工作。 我正在使用 Visual Studio 2013。我创建了一个新的 MVC 5 项目,并认为使用新的 facebook 登录集成会很酷。它在我的 IIS Express 中的 PC 上运行良好。

但是当我将它上传到生产服务器时,我不断收到非常烦人的“No owin.Environment item was found in the context”消息。

这就是我所做的。

  1. 我从未更改过我的项目或程序集的名称。
  2. 程序集名称是 Wodivate
  3. 命名空间是 Wodivate
  4. 我有默认的 Startup.cs 类,其中包含以下内容:

    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. 在 App_Start 中有一个 Startup.Auth.cs 文件,其中包含:

    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. 我的 Web.config 文件包括:

    <add key="owin:AppStartup" value="Wodivate.Startup, Wodivate" />
    <add key="owin:AutomaticAppStartup " value="false" />
    
  7. 我还确保关注 No owin.Environment item was found in the context - only on server 上的帖子并安装了 Microsoft.Owin.Host.SystemWeb

还有其他人坚持这个吗?我已经碰壁了 3 天了。

【问题讨论】:

  • 我也看到了那个帖子,但它对我不起作用。看看是否有人看过所有这些帖子并且仍然卡住或找到不同的方法来解决它。真是太奇怪了..
  • 确保您的 Web.config 确实位于生产服务器上的正确位置...
  • 你终于让它工作了吗?我遇到了同样的问题。肯定找到了启动类,所以这一定是别的东西。最烦人的是,这只发生在生产机器上,而不是本地主机上。

标签: c# visual-studio-2013 owin


【解决方案1】:

我遇到了同样的问题,它是由 IIS 设置引起的。

我的 IIS 部署有一个顶级站点,下面有几个应用程序。您应确保站点级别的“应用程序设置”与您的应用程序不冲突。

在我的例子中,我在站点级别有“owin:AutomaticAppStartup = false”,它被我的应用程序继承了。

总结:

  1. 打开 IIS 管理器
  2. 选择服务器 -> 应用程序设置。确保在此级别没有任何冲突。
  3. 选择站点 -> 应用程序设置。确保在此级别没有任何冲突。


编辑: 我发现您可以简单地在 web.config 中添加以下 appSetting 来覆盖任何潜在的继承冲突。不过,我建议您先了解是否存在冲突,以便您做出有根据的更改。

<add key="owin:AutomaticAppStartup" value="true"/>

【讨论】:

    【解决方案2】:

    我需要另一个 web.config 更改来解决这个问题:

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

    虽然我试过this setting is discouraged

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

    但这给了我错误:

    “由于对象的当前状态,操作无效”

    我还不确定那里发生了什么,无论是错误的模块还是其他什么。

    【讨论】:

      【解决方案3】:

      您是否尝试过这样做?

      <system.webServer>
          <modules runAllManagedModulesForAllRequests="false" />
          <handlers>
              <remove name="StaticFile" />
              <add name="OWIN" path="*" verb="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler" />
          </handlers>
      </system.webServer>
      

      【讨论】:

      【解决方案4】:

      您是否检查了正在使用的应用程序池的模式。 OWIN 适用于应用程序池的集成模式。它不会在经典模式下工作。

      【讨论】:

        【解决方案5】:

        当您收到异常“在上下文中找不到 owin.Environment 项”时。来自Request.GetOwinContext(),通常表示OWIN启动类检测失败。

        确保 IIS 在启动时正在执行 Startup.cs(在根文件夹中)中的代码。要检查它是否在生产服务器上运行,我会推荐以下内容。 1) 插入以下行:

        ...
         System.Diagnostics.Trace.WriteLine("OwinStartup " + this.GetType().Namespace);
         ConfigureAuth(app);
        ...
        

        2) 从此处获取 DebugView 实用程序:https://technet.microsoft.com/en-us/library/bb896647.aspx

        3) 以管理员身份执行 DbgView.exe 并确保启用“Capture / Capture Global Win32”选项。

        4) 当 IIS 启动时,您应该会看到来自上述代码的跟踪消息(通过触摸 web.config 确保它实际上已重新启动)。

        5) 如果没有执行,请看这里:http://www.asp.net/aspnet/overview/owin-and-katana/owin-startup-class-detection

        没有执行的原因有很多。他们中的许多人都在stackoverflow上。我要添加的另一项是:确保您在 web.config 的“编译”节点上没有设置“batch=false”。

        【讨论】:

          【解决方案6】:

          发生的情况是您的 BIN 不包括参考。在首选项中将其更改为“复制本地”为 TRUE。然后再次发布。

          【讨论】:

            【解决方案7】:

            我为此苦苦挣扎了好几天!!尝试了我能找到的每个帖子中的所有内容,这解决了问题。我在 VS 2013 中开发并发布到 Server 2008 虚拟机,但我一直收到相同的“在上下文中找不到 owin.Environment 项目”消息。

            我将此条目放在模块的 web 配置中并修复了它。我不确定我理解为什么它修复了它,但它确实做到了。

                <modules  runAllManagedModulesForAllRequests="true" />
            

            【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-11-10
            • 1970-01-01
            相关资源
            最近更新 更多