隐藏基于角色的链接

chi*_*_82 9 asp.net-mvc asp.net-roles razor asp.net-mvc-3

我是asp.mvc的新手.我正在尝试开发一个维护员工数据的门户.在我的系统中,只有"经理"才能创建员工.如何在管理员登录时启用链接,并在员工登录时禁用.谢谢

我的看法

@model IEnumerable<SealManagementPortal_3._0.Models.VOC_CUSTODIAN>
@{
    ViewBag.Title = "List of Custodians";
}
<h2>Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<script type="text/javascript">
    jQuery(document).ready(function () {
        jQuery("#list2").jqGrid({
            url: '@Url.Action("GridData", "Custodian")',
            datatype: 'json',
            mtype: 'GET',
            colNames: ['Agent ID', 'Branch', 'Unique ID', 'Custodian Name', /*'NRIC No', 'E-Mail', 'Contact No', 'Mobile No',*/'Role', 'Details', 'Edit', 'Delete'],
            colModel: [
                { name: 'Agent ID', index: '', width: 10, align: 'left' },
                { name: 'Branch', index: '', width: 10, align: 'left' },
                { name: 'Unique ID', index: '', width: 10, align: 'left' },
                { name: 'Custodian Name', index: '', width: 10, align: 'left' },                
                {name: 'Role', index: '', width: 10, align: 'left' },
                { name: 'Details', index: '', width: 5, align: 'left' },
                { name: 'Edit', index: '', width: 5, align: 'left' },
                { name: 'Delete', index: '', width: 5, align: 'left'}],
            pager: jQuery('#pager2'),
            rowNum: 10,                
            sortname: 'Id',
            sortorder: "desc",
            viewrecords: true,
            autowidth: true,
            caption: 'Custodians List'
        });
    }); 
</script>
@using (Html.BeginForm())
{
    <table id="list2" class="scroll" cellpadding="0" cellspacing="0"></table>
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 21

你可以使用角色.第一个也是最重要的是装饰应该使用Authorize属性执行更新的控制器操作,并指定用户必须拥有的正确角色才能访问此控制器操作:

[HttpPost]
[Authorize(Roles = "Managers")]
public ActionResult Create(Employee emp)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

一旦服务器上的所有内容都安全,您就可以在视图中执行化妆品,并仅在用户担任Managers角色时显示链接:

@if (User.IsInRole("Managers"))
{
    @Html.ActionLink("Create employee", "Create")
}
Run Code Online (Sandbox Code Playgroud)

您可以查看以下文章,以获取有关表单身份验证和角色的更多信息.