![](/img/trans.png)
[英]How to check user permission Role Access before executing a method in ASP.NET MVC4
[英]Page Permission based on Role ASP.NET MVC4
在允許系統訪問某個特定頁面之前,系統檢查用戶角色的機制的最佳實現方式是什么? 此外,為了在頁面中啟用某些鏈接/操作,例如對於具有“超級”角色的用戶,他們可能能夠刪除/編輯數據,而其他人只能看到它?
為了您的信息,我不使用ASP.NET MVC中的開箱即用的用戶管理(用戶是在嵌入到webapp的.mdf數據庫中創建的),但是我已經開發了自己的用戶模塊(用於身份驗證,注冊)並刪除用戶)。
那么..這個問題的最佳做法是什么?
您可以編寫自定義ValidationAttribute
: http : //msdn.microsoft.com/en-AU/library/system.componentmodel.dataannotations.validationattribute.aspx
基本上,您從ValidationAttribute
繼承,並重寫IsValid()
:
public class IsAnAdminAttribute : ValidationAttribute {
protected override bool IsValid(object obj) {
if (Membership.UserInRole("admin"))
return true; // they can access it
else
return false; // can't access it
}
}
..然后將其應用於控制器操作:
[HttpGet]
[IsAnAdmin]
public ActionResult MyAction() {
// only administrators can access this now
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.