asp.net mvc - How to avoid duplication of authorization code logic -


i've written custom authorization attribute derived system.web.mvc.authorizeattribute. i'm using controllers restrict access features.

public class articlecontroller : controller {     [customauthorize(role.administrator)]     public actionresult delete(int id)     {        // ...     } } 

and works fine. want show or hide html elements according same authorization logic. example, in view "article", want hide action button "delete" if user not administrator. i've written that:

<ul id="menu">    <li>@if (user.isinrole(role.administrator)) {            @html.actionlink("delete", "delete", "article", new { id = article.id }, null)        } </li> </ul> 

it works fine well, creates code logic duplication because need specify twice necessary credientials perform action:

  • in controller block or allow action.
  • in view show or hide action link.

what best way avoid duplication? there way reuse custom authorization attribute views?

a custom helper should best option, like:

@html.secureactionlink("delete", "delete", "article") 

this helper check on kind of service see if current user/role has permission on link.


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -