2012-06-18 52 views
7

Im新到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> 

回答

15

你可以使用的角色。第一和最重要的事情是裝飾是應該執行與Authorize屬性更新並指定正確的角色,用戶必須要訪問這個控制器動作有擁有的控制器動作:

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

一旦一切都在服務器上的安全,你可以做化妝品的視圖,並顯示該鏈接僅當用戶在Managers角色:

@if (User.IsInRole("Managers")) 
{ 
    @Html.ActionLink("Create employee", "Create") 
} 

你可以看看在following article有關窗體身份驗證的更多信息和角色。

+0

有沒有辦法避免重複模板中的「經理」?我想在一個地方進行角色管理,並且授權屬性對我來說非常合適。我可以以某種方式定義一個訪問屬性內容的HtmlHelper嗎? –

相關問題