只要您使用将用户对象置于上下文中的标准身份验证方法,就不会太难。在你看来,使用 Html.RenderPartial 和适当的控制器来完成你想要的这样......
/首页/索引视图:
<div id="menu">
<ul>
<li>Page 1</li>
<li>Page 2</li>
<% Html.RenderPartial("/Home/AdminView"); %>
</ul>
</div>
/首页/管理视图:
<li>Admin 1</li>
<li>Admin 2</li>
/共享/空视图:
<!--Empty view for non-viewable admin stuff-->
/家庭控制器:
public ActionResult AdminView()
{
//Check if user is admin
//This should be your own logic... I usually have a few static methods that the
//Users or User object has to assist with checking
if (Models.User.IsAdmin(HttpContext.User.Identity.Name))
{
return View();
}
else
{
return View("Empty");
}
}
这应该工作......我就是这样做的。如果错了,希望更聪明的人发布更好的答案,这样我们都可以学习
编辑:
只是想到了别的东西...如果您的用户对象实现了 IPrincipal,您可以从上下文中提取它,将其转换为您的用户类型并在用户类中拥有信息...
有点像这样
class User : IPrincipal
{
//Implement IPrincipal stuff
public string Role { get; set; }
}
然后管理视图逻辑可能如下所示:
public ActionResult AdminView()
{
//Check if user is admin
//This should be your own logic... I usually have a few static methods that the
//Users or User object has to assist with checking
if ( ((Model.User)HttpContext.User).Role =="Admin" )
{
return View();
}
else
{
return View("Empty");
}
}