【问题标题】:Resolve No owin.Environment item was found in the context解决在上下文中找不到 owin.Environment 项
【发布时间】:2017-05-28 03:03:38
【问题描述】:

我发现很多线程都在讨论这个问题,但到目前为止,让它们中的任何一个工作都让我望而却步。

我一直在尝试关注ProDinner,以此来学习如何在我的应用中实现基于角色的身份验证。但我无法弄清楚我错过了什么。每次我尝试登录时都会得到 ​​p>

在上下文中找不到 owin.Environment 项

经过大量调试和搜索,我发现 Startup 类没有被调用。我试图通过单步执行 proDinner 代码来解决问题,以查看 Startup 是如何被调用和搜索的(很多!)。我的搜索导致我添加(尽管 proDinner 项目中都不存在)

<add key="owin:AutomaticAppStartup" value="Tjs.Startup, Tjs" />

然后

<add key="owin:AutomaticAppStartup" value="Tjs.Web.Admin.Startup, Tjs" />

下面我添加了我认为是相关代码的所有内容,但如果我遗漏任何内容,请告诉我。

AccountController 是 ProDinner 的复制粘贴,加上一个无参数构造函数,我无法弄清楚该参数是如何在 Prodinner 项目中传递的(这可能是问题吗?)

Tjs:解决方案名称

Tjs.Web.Admin:命名空间

Owin 包

<package id="Microsoft.Owin" version="3.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net45" />

项目的根目录

using Microsoft.Owin;
using Owin;

using Tjs.Web.Admin;
using Tjs.Web.Admin.App_Start;

[assembly: OwinStartup(typeof(Startup))]
namespace Tjs.Web.Admin
{
  public class Startup
  {
    public void Configuration(IAppBuilder app)
    {
        OwinConfig.ConfigureAuth(app);
    }
  }
}

App_Start

using Microsoft.AspNet.Identity;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Owin;
namespace Tjs.Web.Admin.App_Start
{
  public static class OwinConfig
  {
    public static void ConfigureAuth(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/SignIn")
        });
    }
  }
}

帐户控制器

    private readonly IUserService userService;
    public AccountController()
    {

    }

    public AccountController(IUserService userService)
    {
        this.userService = userService;
    }

    private IAuthenticationManager Authentication
    {
        get
        {
            return HttpContext.GetOwinContext().Authentication;
        }
    }

    private void SignInOwin(string name, bool rememberMe, string role)
    {
        var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, name) },
            DefaultAuthenticationTypes.ApplicationCookie,
                ClaimTypes.Name, ClaimTypes.Role);

        //foreach (var role in roles)
        //{
        //    identity.AddClaim(new Claim(ClaimTypes.Role, role));
        //}
        identity.AddClaim(new Claim(ClaimTypes.Role, role));

        Authentication.SignIn(new AuthenticationProperties
        {
            IsPersistent = rememberMe
        }, identity);
    }

    public ActionResult SignIn()
    {
        return View(new SignIn { Email = "o", Password = "1" });
    }

    [HttpPost]
    public ActionResult SignIn(SignIn input)
    {
        if (!ModelState.IsValid)
        {
            input.Password = null;
            input.Email = null;
            return View(input);
        }

        Staff staff;

        //ACHTUNG: remove this in a real app
        if (input.Email == "o" && input.Password == "1")
        {
            staff = new Staff { Email = "o", Role = new Role { Description = "admin" } };
        }
        else
        {
            staff = userService.Get(input.Email, input.Password);
        }

        if (staff == null)
        {
            ModelState.AddModelError("", "Try Login: o and Password: 1");
            return View();
        }

        SignInOwin(staff.Email, input.Remember, staff.Role.Description);


        return RedirectToAction("index", "home");
    }

    public ActionResult SignOff()
    {
        Authentication.SignOut();
        return RedirectToAction("SignIn", "Account");
    }
}

失败了

private IAuthenticationManager Authentication
{
    get
    {
        return HttpContext.GetOwinContext().Authentication;
    }
}

【问题讨论】:

  • 试着把这个添加到你web.config&lt;appSettings&gt;&lt;add key="owin:AppStartup" value="[Namespace].Startup, [AssemblyName]" /&gt;&lt;/appSettings&gt;
  • 我错过了什么?我以为这是我尝试过的事情之一? (在我的帖子顶部附近提到)
  • key="owin:AppStartup"
  • 谢谢!!!!我一定在某个时候失明了,我不敢相信我没有发现。我的程序集名称也错了!

标签: c# asp.net-mvc authentication owin


【解决方案1】:

将此添加到您的web.config &lt;appSettings&gt;&lt;add key="owin:AppStartup" value="[Namespace].Startup, [AssemblyName]" /&gt;&lt;/appSettings&gt;

【讨论】:

  • 我还需要将 Startup.cs 类文件添加到我的项目中(右键单击 Project > Add > Class > 搜索 Owin Startup 类并命名为“Startup.cs”)
  • 我还必须删除 如在 Mathi Rajan 的回答中在 stackoverflow.com/questions/18232549/... 中提到的。
【解决方案2】:

我已经添加到“web.config”中了

<modules runAllManagedModulesForAllRequests="true">

【讨论】:

  • 你把这个放在哪里了?它不能直接作为&lt;configuration&gt; 元素下的子元素。
  • 对不起,我说得太早了。我看到了红色的错误下划线,并认为它在抱怨一个不合适的元素。事实上,问题在于标签没有关闭:&lt;modules runAllManagedModulesForAllRequests="true" /&gt;。所以它确实直接位于&lt;configuration&gt; 元素下。
【解决方案3】:

我遇到了同样的问题,我在 WebConfig 中添加了一个新密钥 owin:AutomaticAppStartup,以便自动加载 Startup 类。

<add key="owin:AppStartup" value="[Namespace].Startup,[AssemblyName]" />
<add key="owin:AutomaticAppStartup" value="true"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    相关资源
    最近更新 更多