解决问题.在上下文中找到环境项

本文关键字:环境 上下文 解决问题 | 更新日期: 2023-09-27 18:35:27

我发现很多线程都在讨论这个问题,但到目前为止,我还没有找到任何一个线程。

我一直在尝试跟随ProDinner学习如何在我的应用程序中实现基于角色的身份验证。但我无法计算出我遗漏了什么。每次我尝试登录时,我都会收到

没有。在上下文中找到环境项

经过大量的调试和搜索,我发现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);
    }
  }
}

应用程序启动

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

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

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

我已将其添加到"web.config"中

<modules runAllManagedModulesForAllRequests="true">