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

ton*_*9uk 2 c# authentication asp.net-mvc owin

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

我一直在尝试关注ProDinner,以此来学习如何在我的应用中实现基于角色的身份验证.但我无法解决我所缺少的问题.我每次尝试登录都会得到

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

经过大量的调试和搜索,我发现这个Startup类没有被调用.我试图通过逐步调试proDinner代码来解决,如何Startup被调用和googled(很多!).我的搜索引导我添加(虽然在proDinner项目中都不存在)

<add key="owin:AutomaticAppStartup" value="Tjs.Startup, Tjs" />
Run Code Online (Sandbox Code Playgroud)

然后

<add key="owin:AutomaticAppStartup" value="Tjs.Web.Admin.Startup, Tjs" />
Run Code Online (Sandbox Code Playgroud)

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

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" />
Run Code Online (Sandbox Code Playgroud)

项目的根源

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);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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")
        });
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

的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");
    }
}
Run Code Online (Sandbox Code Playgroud)

它失败了

private IAuthenticationManager Authentication
{
    get
    {
        return HttpContext.GetOwinContext().Authentication;
    }
}
Run Code Online (Sandbox Code Playgroud)

Ind*_*ons 5

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

  • 我还需要将Startup.cs类文件添加到我的项目中(右键单击Project&gt; Add&gt; Class&gt;搜索Owin Startup类,并将其命名为'Startup.cs') (2认同)