Examples illustrate simple role rights control in asp.net-practical skills

Source: Internet
Author: User

Role table + User table, where Hope_t_user (user) in the User_roleid Association Hope_t_role (role) of role_id, the table in the fields are simple common English, not to be detailed.

The main idea is to use the public class in which the role of the operation of the ultra vires, and then all the pages need to verify the permissions of the page to inherit this class, use the corresponding parameters to determine the role to do the appropriate action.

public class: Adminpermission

Copy Code code as follows:

<summary>
Adminpermission Role Permission Validation class
</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;//Whether access is allowed
if (session["hopeuser"] = = null)
{
Isallowaccess = false;
When}//session does not exist
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 (\ "Sorry, you do not have permission to view this page" + role. Role_managead + ". \ "); return false;</script>");
}
}
}
/**////<summary>
Role Permissions
</summary>
public enum Enumrole
{
#region
/**////<summary>
Whether to allow articles to be published
</summary>
Role_postarticle=1,
/**////<summary>
Whether to allow administration of articles
</summary>
Role_managearticle,
/**////<summary>
Whether to allow management of links
</summary>
Role_managelink,
/**////<summary>
Whether to allow management of subordinate information
</summary>
Role_manageinfo,
/**////<summary>
Whether to allow management logs
</summary>
Role_managelog,
/**////<summary>
Whether to allow management system
</summary>
Role_managesystem,
/**////<summary>
Whether to allow administrative voting
</summary>
Role_managevote,
/**////<summary>
Whether to allow administrative users
</summary>
Role_manageuser,
/**////<summary>
Whether to allow administrative roles
</summary>
Role_managerole,
/**////<summary>
Whether to allow management of ads
</summary>
Role_managead,
/**////<summary>
Management issued documents
</summary>
Role_managedoc,
/**////<summary>
Manage interest rates
</summary>
Role_managerate
#endregion
}

Add the following code to the page you want to verify
Copy Code code as follows:

Public partial class manage_manage_user_add:adminpermission//inheriting common classes
{
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
Checkuserpermission (Enumrole.role_manageuser);//Verify Permissions
}
}
...
}

This role privilege module is a simple module, which can be subdivided into role-user-role function (-single user function) model according to the actual project requirement.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.