執行個體說明asp.net中的簡單角色許可權控制_實用技巧

來源:互聯網
上載者:User

角色表+使用者表,其中Hope_T_User(使用者)中的User_RoleId關聯Hope_T_Role(角色)的Role_Id,表中欄位均為簡單常見英文,不作詳解。

主要思路是使用公用類在其中根據角色得出其越權時的操作,然後後台所有需要驗證許可權的頁面均繼承此類,使用相應參數判斷角色以做相應操作。

公用類:AdminPermission

複製代碼 代碼如下:

/// <summary>
/// AdminPermission角色許可權驗證類
/// </summary>
public class AdminPermission : System.Web.UI.Page
{
Hope.BLL.Hope_T_Role bllRole = new Hope.BLL.Hope_T_Role();
protected void CheckUserPermission( enumRole objEnumRole )
{
bool isAllowAccess=false;//是否允許訪問
if (Session["HopeUser"] == null)
{
isAllowAccess = false;
}//Session不存在時
Hope.Model.Hope_T_User objUser= (Hope.Model.Hope_T_User)Session["HopeUser"];
int roleId=0;
if (objUser != null)
{
roleId = Convert.ToInt32(objUser.User_RoleId);
}
Hope.Model.Hope_T_Role role = bllRole.GetModel(roleId);

if (role == null)
{
isAllowAccess = false;
}
else
{
switch (objEnumRole)
{
#region
case (enumRole.Role_PostArticle):
if (role.Role_PostArticle != "1")
isAllowAccess = false;
else
isAllowAccess = true;
break;
case (enumRole.Role_ManageArticle):
if (role.Role_ManageArticle != "1")
isAllowAccess = false;
else
isAllowAccess = true;
break;
case (enumRole.Role_ManageInfo):
if (role.Role_ManageInfo != "1")
isAllowAccess = false;
else
isAllowAccess = true;
break;
case (enumRole.Role_ManageLink):
if (role.Role_ManageLink != "1")
isAllowAccess = false;
else
isAllowAccess = true;
break;
case (enumRole.Role_ManageLog):
if (role.Role_ManageLog != "1")
isAllowAccess = false;
else
isAllowAccess = true;
break;
case (enumRole.Role_ManageRole):
if (role.Role_ManageRole != "1")
isAllowAccess = false;
else
isAllowAccess = true;
break;
case (enumRole.Role_ManageSystem):
if (role.Role_ManageSystem != "1")
isAllowAccess = false;
else
isAllowAccess = true;
break;
case (enumRole.Role_ManageUser):
if (role.Role_ManageUser != "1")
isAllowAccess = false;
else
isAllowAccess = true;
break;
case (enumRole.Role_ManageVote):
if (role.Role_ManageVote != "1")
isAllowAccess = false;
else
isAllowAccess = true;
break;
case (enumRole.Role_ManageAd):
if (role.Role_ManageAd != "1")
isAllowAccess = false;
else
isAllowAccess = true;
break;
case (enumRole.Role_ManageDoc):
if (role.Role_ManageDoc != "1")
isAllowAccess = false;
else
isAllowAccess = true;
break;
case (enumRole.Role_ManageRate):
if (role.Role_ManageRate != "1")
isAllowAccess = false;
else
isAllowAccess = true;
break;
#endregion
}
}
if (isAllowAccess == false)
{
Response.Write("<script type=\"text/javascript\">alert(\"抱歉,您無許可權查看此頁" + role.Role_ManageAd + "。\");return false;</script>");
}
}
}
/**//// <summary>
/// 角色許可權
/// </summary>
public enum enumRole
{
#region
/**//// <summary>
/// 是否允許發布文章
/// </summary>
Role_PostArticle=1,
/**//// <summary>
/// 是否允許管理文章
/// </summary>
Role_ManageArticle,
/**//// <summary>
/// 是否允許管理連結
/// </summary>
Role_ManageLink,
/**//// <summary>
/// 是否允許管理附屬資訊
/// </summary>
Role_ManageInfo,
/**//// <summary>
/// 是否允許管理日誌
/// </summary>
Role_ManageLog,
/**//// <summary>
/// 是否允許管理系統
/// </summary>
Role_ManageSystem,
/**//// <summary>
/// 是否允許管理投票
/// </summary>
Role_ManageVote,
/**//// <summary>
/// 是否允許系統管理使用者
/// </summary>
Role_ManageUser,
/**//// <summary>
/// 是否允許管理角色
/// </summary>
Role_ManageRole,
/**//// <summary>
/// 是否允許管理廣告
/// </summary>
Role_ManageAd,
/**//// <summary>
/// 管理下發檔案
/// </summary>
Role_ManageDoc,
/**//// <summary>
/// 管理利率
/// </summary>
Role_ManageRate
#endregion
}

需要驗證的頁面中添加如下代碼
複製代碼 代碼如下:

public partial class manage_manage_user_add :AdminPermission//繼承公用類
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
CheckUserPermission(enumRole.Role_ManageUser);//驗證許可權
}
}
//…
}

此角色許可權模組為簡單模組,可根據實際項目需求再對角色功能進行細化,使之成為角色-使用者-角色功能(-單一使用者功能)模型。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.