2016-02-19 119 views
2

我發現許多線程都在討論這個問題,但是讓它們中的任何一個工作到目前爲止還是沒有。解決在上下文中找不到owin.Environment項目

我一直試圖按照ProDinner作爲一種學習如何在我的應用程序中實現基於角色的認證的方式。但我無法弄清楚我缺少的東西。每次我試圖登錄我得到

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

的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; 
    } 
} 
+0

嘗試將此添加到您的web.config'' ' –

+0

我錯過了什麼?我認爲這是我嘗試過的事情之一?(提到我的帖子頂部) – tony09uk

+0

'key =」owin:AppStartup「 –

回答

5

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

+1

我還需要將Startup.cs類文件添加到我的項目中(右鍵單擊Project > Add> Class>搜索Owin Startup類和名稱'Startup.cs') – JasonH

+0

我還必須刪除如上所述在Mathi Rajan的回答中http://stackoverflow.com/questions/18232549/no-owin-environment-item-was-found-in-the-context。 –

0

我已經添加到的 「web.config」

<modules runAllManagedModulesForAllRequests="true"> 
0

我有同樣的問題,我添加了一個新的鍵入owin:AutomaticAppStartup到WebConfig以便啓動類自動加載。

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