. NET permission management and. net permission management

Source: Internet
Author: User

. NET permission management and. net permission management

Preface: There are many application permission management functions in various aspects for powerful back-end website management. Taking the internal management system of the company as an example, administrators can grant different permissions to different departments of different employees, or implement "Pyramid" Management Based on the parent-subordinate affiliation (note: the granted permission management mode cannot be dynamically edited, that is, the level-2 and above permissions cannot be divided ). I suggest you correct the content of this article.

Body:
Check whether a user has the permission to use checkbox status to differentiate storage.

(1) Storage
Here, the permission function (Category) Name and permission module (specific) name are manually entered into the database and cannot be added, deleted, modified, and queried. In this example, a permission module is used as the most basic element, and whether the permission is expressed as 1 or 0. Then, all the permissions of the current user are linked to a binary string and stored in the database. Example code 1.1

(2) readingExample code 1.2

(3) the front-end code is as follows:

// Add a comma and a vertical bar using js, and pass the value <script language = "javascript" type = "text/javascript"> function getcheck () {var inputs = document through hidden. getElementById ("cbpanel"); var chks = inputs. getElementsByTagName ("input"); var str = ""; for (var I = 0; I <chks. length; I ++) {if (chks [I]. type = 'checkbox') {str + = chks [I]. value + "," + chks [I]. checked + "|" ;}} document. getElementById ("Hidden1 "). value = str ;}< script> <body> <div id = "cbpanel"> <asp: Literal ID = "LiteralRole" runat = "server"> </asp: literal> <input id = "Hidden1" runat = "server" type = "hidden"/> </div> </body>
Sample Code 1.2 // <summary> // bind all module categories /// </summary> private void BindAllModule () {StringBuilder sb = new StringBuilder (); sb. append ("<table class = \" AddRole \ "width = \" 100% \ "border = \" 0 \ "cellpadding = \" 0 \ "cellspacing = \" 0 \"> "); bll_User_Module created L = new Bll_User_Module (); IList <User_Model.Model_User_Module> list = created L. getAllList (); // obtain the string moduleidstr = ""; for (int I = 0; I <list. count; I ++) {Sb. append ("<tr> <th style = \" width: 100px; height: 36px; \ ">"); sb. append (list [I]. moduleName); sb. append ("</th>"); sb. append ("<td>"); sb. append ("{" + list [I]. ID + "}"); sb. append ("</td> </tr>"); moduleidstr + = list [I]. ID. toString () + ",";} sb. append ("</table>"); if (Request. queryString ["nid"]! = Null) {// modify BLL_User_Role mrbll = new BLL_User_Role (); User_Model.Model_User_Role mrmodel = mrbll. getModel (Convert. toInt32 (Request. queryString ["nid"]); string RoleActions = mrmodel. roleAction; BindAllFunction (moduleidstr, sb, RoleActions);} else {// added BindAllFunction (moduleidstr, sb ,"");}} /// <summary> /// bind each function option in each module /// </summary> /// <param name = "moduleidstr"> Number of modules </param> /// <param name = "sb"> html table </param> /// <param name = "RoleActions"> permission binary string </param> private void BindAllFunction (string moduleidstr, stringBuilder sb, string RoleActions) {Bll_User_Function bll = new Bll_User_Function (); IList <User_Model.Model_User_Function> list = bll. getAllList (); // a menu generic set moduleidstr = moduleidstr. trimEnd (','); // number of functional modules separated by commas string [] str = moduleidstr. split (','); // remove the comma and save it as an array (int I = 0; I <str. length; I ++) // The loop Array {StringBuilder funsb = new StringBuilder (); for (int j = 0; j <list. count; j ++) {if (list [j]. moduleNo = Convert. toInt32 (str [I]) {if (RoleActions = "") {// Add funsb. append ("<input id = \" "+ list [j]. funcNo. toString () + "\" type = \ "checkbox \" checked = \ "checked \" value = \ "" + list [j]. funcNo. toString () + "\"/> "); funsb. append (list [j]. funcName + "& nbsp;") ;}else {// modify if (RoleActions. length> = list [j]. funcNo) {// FuncNO indicates that the current permission is in the binary character index value if (RoleActions. substring (list [j]. funcNo-1, 1) = "1") // intercept the permission binary string and determine its status {// select the status funsb. append ("<input id = \" "+ list [j]. funcNo. toString () + "\" type = \ "checkbox \" checked = \ "checked \" value = \ "" + list [j]. funcNo. toString () + "\"/> "); funsb. append (list [j]. funcName + "& nbsp;") ;}else {// unselected funsb. append ("<input id = \" "+ list [j]. funcNo. toString () + "\" type = \ "checkbox \" value = \ "" + list [j]. funcNo. toString () + "\"/> "); funsb. append (list [j]. funcName + "& nbsp;") ;}} else {funsb. append ("<input id = \" "+ list [j]. funcNo. toString () + "\" type = \ "checkbox \" value = \ "" + list [j]. funcNo. toString () + "\"/> "); funsb. append (list [j]. funcName + "& nbsp;") ;}}// replace sb. replace ("{" + str [I] + "}", funsb. toString ();} LiteralRole. text = sb. toString () ;}// save (update) the event protected void Submit_Click (object sender, EventArgs e) {string cbstr = Hidden1.Value; // obtain the value of the foreground hidden, such as "1, true | 2, true | 3, false | 4, true "format ........}
Sample Code 1.1 /// <summary> /// generate the role binary string /// </summary> /// <param name = "str"> </param> // /<returns> binary sequence </returns> private string GenerateRoleAction (string str) {// virtual table build two columns storage permission serial number and its status DataTable dt = new DataTable (); DataColumn col1 = new DataColumn ("funno", typeof (int); dt. columns. add (col1); DataColumn col2 = new DataColumn ("flag"); dt. columns. add (col2); string [] strarray = str. trimEnd ('| '). split ('|'); // intercept a vertical line between any two binary elements for (int I = 0; I <strarray. length; I ++) {DataRow dr = dt. newRow (); dr [0] = strarray [I]. split (',') [0]; // permission serial number dr [1] = strarray [I]. split (',') [1]; // permission status. Use true or false to record dt. rows. add (dr);} dt. defaultView. sort = "funno asc"; DataTable dttemp = dt. defaultView. toTable (); // convert a Status column to a binary string s = ""; for (int j = 0; j <dttemp. rows. count; j ++) {if (dttemp. rows [j] ["flag"]. toString (). toLower () = "true") {s + = "1"; // select the status (true, that is, this permission is granted) use 1 to indicate} else {s + = "0"; // unselected (false, indicating that this permission is not granted) use 0 to indicate} return s; // return the binary string indicating the permission}

 

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.