开发者

Disable link if user is not allowed to access target

I have created an ASP.NET MVC application and created different kind of roles for my users. I have then created different kinds of AuthorizeAttributes for allowing/disallowing access to different Actions in开发者_运维知识库 my controls.

However I have got a lot of links that points to different of theese actions that are restricted for different roles. Can you somehow fix so that theese links get disabled automatically? I could of course add a lot of UserIsInRole(....)-stuff in my code but I really would prefer not to if there is a better way.

Du you have any suggestions?


Are they in a list or menu? Is this something your controller could pass to your View? you could pass out a list of all allowed (or forbidden, whichever is more appropriate) and check that before you display a link.

if (allowedLink.Contans(myLink)
// show enabled link
else
//show disabled

The other good way would be to override the HtmlHelper for ActionLinks and make it do the check for permissions for you. Then if they do not have permissions, your html helper would display it disabled.

For examples, see this link http://www.asp.net/learn/mvc/tutorial-09-cs.aspx


Off the top of my head...

• You can set the onClick action for each link to do nothing.

• You can set the URL for the link to "#", which does nothing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜