解决在环境中未找到owin.Environment项目 [英] Resolve No owin.Environment item was found in the context

查看:337
本文介绍了解决在环境中未找到owin.Environment项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现有很多讨论这个问题的话题,但是到目前为止,让他们中的任何一个都无法工作.

I have found many threads discussing this problem but getting any of them to work has eluded me so far.

我一直在尝试 ProDinner 作为学习如何在我的应用中实现基于角色的身份验证的一种方式.但是我无法解决我所缺少的东西.每次尝试登录时,我都会得到

I have been trying to follow ProDinner as a way of learning how to implement role based authentication in my app. But I cannot work out what I am missing. Every time I try to log in I get

在上下文中未找到owin.Environment项目

No owin.Environment item was found in the context

经过大量调试和搜索,我发现未调用Startup类.我试图通过逐步解决proDinner代码来解决问题,以查看Startup的调用方式和谷歌搜索方式(很多!).我的搜索使我不得不添加(尽管proDinner项目中都不存在)

After a lot of debugging and searching I have found the Startup class is not being called. I've tried to resolve by stepping through the proDinner code to see, how Startup is being called and googled (a lot!). My searches lead me to adding (although neither exist in the proDinner project)

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

然后

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

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

Below I have added all of what I believe is the relevant code, but please let me know if I am missing anything.

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

The AccountController is a copy paste of ProDinner, with the addition of a parameterless constructor, I was unable to work out how that parameter is being passed in the Prodinner project (could that be the problem?)

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")
        });
    }
  }
}

AccountController

    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 <appSettings><add key="owin:AppStartup" value="[Namespace].Startup, [AssemblyName]" /></appSettings>

这篇关于解决在环境中未找到owin.Environment项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆