2016-11-16 48 views
0

嘗試創建基於我的整個站點的基礎控制器,因爲它基於用戶角色動態加載導航欄。OWIN驗證BaseController上的HTTPContext NULL

的問題是,它似乎總是加載,並得到用戶的角色,即使owin還沒有被載入和用戶尚未登陸,

下面是一個名爲LayoutController

[Authorize] 
public class LayoutController : Controller 
{ 
    public List<NavigationMenuModel> MainLayoutViewModel { get; set; } 

    public LayoutController() 
    { 
     this.MainLayoutViewModel = new List<NavigationMenuModel>(); 
     using (var context = new OperationalDataContext()) 
     { 
    //The BELOW LINE IS ISSUE 
      var username = HttpContext.GetOwinContext().Authentication.User.Identity.Name; 
      var pages = context.GET_PAGES_BY_USERNAME(username); 
      var pagesTop = pages.Where(x => x.Parent == null); 
      foreach (var page in pagesTop) 
      { 
       var tmpNM = new NavigationMenuModel(); 
       tmpNM.DisplayName = page.Name; 
       tmpNM.RelativeUrl = page.RelativeUrl; 
       var children = pages.Where(x => x.Parent != null && x.Parent.Equals(page.Name) && x.Site.Equals("PRODUCT")); 
       List<NavigationMenuModel> tmpChildren = new List<NavigationMenuModel>(); 
       foreach (var child in children) 
       { 
        var tmpC = new NavigationMenuModel(); 
        tmpC.DisplayName = child.Name; 
        tmpC.RelativeUrl = child.RelativeUrl; 
        var children1 = pages.Where(x => x.Parent != null && x.Parent.Equals(child.Name) && x.Site.Equals("PRODUCT")); 
        List<NavigationMenuModel> tmpChildren1 = new List<NavigationMenuModel>(); 
        foreach (var child1 in children) 
        { 
         var tmpC1 = new NavigationMenuModel(); 
         tmpC1.DisplayName = child1.Name; 
         tmpC1.RelativeUrl = child1.RelativeUrl; 
         tmpChildren1.Add(tmpC1); 
        } 
        tmpC.Children = tmpChildren1; 
       } 
       tmpNM.Children = tmpChildren; 
       this.MainLayoutViewModel.Add(tmpNM); 
      } 
     } 
     this.ViewBag["MainLayoutViewModel"] = this.MainLayoutViewModel; 
    } 
} 
的BaseController

再有就是dashboardController(主頁)

public class DashboardController : LayoutController 
{ 
    // GET: Dashboard 
    public ActionResult Index() 
    { 
     return View("Index"); 
    } 
} 

我們有一個簡單的基於cookie的登錄是允許anayomous

public class AccountController : Controller 
{ 
    IAuthenticationManager AuthenticationManager 
    { 
     get { return HttpContext.GetOwinContext().Authentication; } 
    } 

    [HttpGet] 
    [AllowAnonymous] 
    [Route("login")] 
    public ActionResult Login() 
    { 
     return View("Login"); 
    } 
} 
} 

在startup.css

public void ConfigureAuthentication(IAppBuilder app) 
    { 
     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      LoginPath = new PathString("/Login"), 
     }); 
    } 

的問題似乎是,它不會重定向試圖去佈局之前登錄。

AccountController不引用LayoutController。

回答

0

問題是,你正在做你在控制器的構造函數中做的事情。

如果用戶登錄,Authorize屬性會檢查每種方法,如果不是,則將其重定向到登錄頁面。在任何方法被調用之前,構造器代碼在創建Controller之前執行。

在您的LayoutController上覆蓋OnActionExecuting並將構造函數代碼移到那裏。我不太確定Authorize屬性是否在執行OnActionExecuting之前攔截對控制器方法的調用,但我會試一試。

順便說一句:AllowAnonymous用於反轉單個方法的控制器Authorize屬性。因此,如果您的AccountController尚未設置此屬性,則在AllowAnonymous中沒有用處。但這有點偏離主題。

希望它有幫助。