嘗試創建基於我的整個站點的基礎控制器,因爲它基於用戶角色動態加載導航欄。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。