我了解您可以使用表单例份验证根据您选择的标准授予/拒绝对某些页面的访问。
但是,我希望比这更具体一点,并说,根据他们的权限为用户显示不同的按钮。
我知道我可以做类似的事情
if(((User)ViewData["CurrentUser"]).IsEmployee).....
但这似乎不是很优雅,并且很快就会变得困惑。
是否有任何指南/工具/框架功能可以帮助我?
最佳答案
使用基于角色的身份验证,然后适本地设置角色。然后你可以做这样的事情:
if (ViewContext.HttpContext.User.IsInRole("vEmployee") {
这样做的好处是它是 ASP.NET 的核心功能——甚至不是特定于 MVC 的——所以它将与每个可能的成员资格提供者一起工作。
然后,您可以为要有条件地显示的任何控件添加 View 助手重载:
public static string TextBox(this HtmlHelper helper,
string name, string value, string role, object htmlAttributes)
{
if helper.ViewContext.HttpContext.User.IsInRole(role) {
return helper.TextBox(name, value, htmlAttributes);
}
else
{
return null;
}
}
...并称之为:
<%= Html.TextBox("name", "value", "vEmployee", null) %>
关于asp.net-mvc - ASP MVC View 中的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/370884/