我真的无法理解我必须实现的功能之一。客户可以购买一堆插件(插件列表在db表中)。我必须实现只显示客户付费的插件的功能。
我知道我可以在视图中创建一堆if语句,但这感觉像是一个有点"黑客"的解决方案,这将是一个痛苦的工作。
实现这些功能的正确方法是什么?
由于您谈论的是功能而不是插件,我建议您考虑使用AuthorizeAttribute和角色来控制对每个功能和/或功能集的访问。
public class FeatureController : Controller
{
[Authorize(Roles = "Feature1")]
public ActionResult Feature1()
{
// Implement feature here...
return View();
}
[Authorize(Roles = "Feature1")]
[HttpPost]
public ActionResult Feature1(Model model)
{
// Implement feature here...
return View();
}
[Authorize(Roles = "Feature2")]
public ActionResult Feature2()
{
// Implement feature here...
return View();
}
// Other features...
}
然后,任何处于Feature1
角色的用户都将有权访问Feature1
,Feature2
角色将有权使用Feature2
,等等。
您可以将AuthorizeAttribute
与MvcSiteMapProvider的安全修剪功能结合起来,使菜单项只有在用户担任相应角色时才可见。
全面披露:我是MvcSiteMapProvider
的主要贡献者。