Hide Role Based Link

Im new to asp.mvc. I am trying to develop a portal for storing employee data. In my system, only the "Manager" has settings for creating an employee. How to enable the link when the manager logs in and disconnects when logging in. thanks

My view

@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> 
+7
source share
1 answer

You can use roles. The first and most important thing is to decorate the controller action that must be updated with the Authorize attribute and specify the correct roles that the user must have to access this controller action:

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

Once everything is protected on the server, you can make cosmetics in the view and show the link only if the user is in the Managers role:

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

You can see the following article for more information about authentication and form roles.

+16
source

All Articles