gpt4 book ai didi

asp.net-mvc - 根据角色隐藏链接

转载 作者:行者123 更新时间:2023-12-04 02:56:25 26 4
gpt4 key购买 nike

我是 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>

最佳答案

你可以使用角色。第一最重要的事情是用 Authorize 装饰应该执行更新的 Controller Action 属性并指定用户必须拥有的正确角色才能访问此 Controller 操作:

[HttpPost]
[Authorize(Roles = "Managers")]
public ActionResult Create(Employee emp)
{
...
}

一旦服务器上的一切都是安全的,您就可以在 View 中进行修饰并仅当用户在 Managers 中时才显示链接。角色:
@if (User.IsInRole("Managers"))
{
@Html.ActionLink("Create employee", "Create")
}

你可以看看 following article有关表单例份验证和角色的更多信息。

关于asp.net-mvc - 根据角色隐藏链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11078423/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com