在BaseController

时间:2016-11-16 19:48:32

标签: c# asp.net-mvc owin

尝试创建一个基于用户角色动态加载导航栏的整个站点继承的基本控制器。

问题是,即使owin尚未加载且用户尚未登录,它似乎总是加载并尝试获取用户角色。

下面是名为LayoutController的BaseController

 [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;
    }
}

然后是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。

1 个答案:

答案 0 :(得分:0)

问题是,你正在做你在控制器构造函数中做的事情。

如果用户已登录,则Authorize属性会检查每个方法,如果不是,则将他/她重定向到登录页面。在创建Controller之前执行构造函数代码 - 在调用任何方法之前。

覆盖OnActionExecuting上的LayoutController并移动构造函数代码。我不太确定Authorize属性是否在执行 OnActionExecuting之前拦截了对控制器方法的调用,但我会尝试一下。

顺便说一句:AllowAnonymous用于反转单个方法的控制器Authorize属性。因此,如果AccountController没有设置此属性,则AllowAnonymous无效。但这有点偏离主题。

希望它有所帮助。