Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / MySQL

nKnight - Role Based Access Control(RBAC) Solution for C#.NET

5.00/5 (3 votes)
7 Apr 2014CPOL4 min read 28.5K   2  
nKnight - RBAC Solution for C#.NET

1. Introduction

First, I want to give a general idea about RBAC. As everybody knows, Role-based access control (RBAC) is a method of regulating access to computer or network resources based on the roles of individual users within an enterprise. In this context, access is the ability of an individual user to perform a specific task, such as view, create, or modify a file. Roles are defined according to job competency, authority, and responsibility within the enterprise.

When properly implemented, RBAC enables users to carry out a wide range of authorized tasks by dynamically regulating their actions according to flexible functions, relationships, and constraints. This is in contrast to conventional methods of access control, which grant or revoke user access on a rigid, object-by-object basis. In RBAC, roles can be easily created, changed, or discontinued as the needs of the enterprise evolve, without having to individually update the privileges for every user.

With role-based access control, access decisions are based on the roles that individual users have as part of an organization. Under the RBAC framework, users are granted membership into roles based on their competencies and responsibilities in the organization. The operations that a user is permitted to perform are based on the user's role.

So in any system, there are the following entities:

  1. Users
  2. Roles
  3. Resources

Programmer needs to write a lot of code to implement RBAC system.

So I thought of writing an open source SDK which can help the programmer to introduce RBAC system easily...

2. nKnight General Notes

2.1 About

nKnight is a toolkit that can be integrated seamlessly with your .NET Windows application. I am sure you guys know what RBAC is, however, in case you forgot...Role-based access control (RBAC) is a method of giving access to computer application resources based on the roles of individual users or groups. In this context, access to a resource is the ability of an individual user to perform a specific task, such as view, create, delete or modify. Roles are defined according to authority and responsibility of the user.

2.2 Architecture of nKnight

nKnight contains the following:

  1. Securitylayer: This will validate user name and password and activate your RBAC system.
  2. Datalayer: This will query your RBAC database. And also creates the tables for the first time.
  3. nKnight Controls: Set of controls, which will automatically tell you that the logged in user has access on the control or not.
  4. nKnight forms: Those forms will help to create user, role and resources and also map it.

2.3 How to Use nKnight into Your Project

There are the following .dll files,

  1. nKnight.dll
  2. nKnightD.dll
  3. nKnightControls.dll

You can add DLLs 1 and 2 from “Add Reference” in Visual Studio. And if you want to use nKnight controls, then from toolbox, you have to choose the 3rd DLL.

Now you have to tell nKnight which database you are using by sending an open connection to the nKnight datalayer. The database holds the matrix of the roles and their authorizations. The first thing that you need to have is a MySQL installation and access to the database. Open up a valid connection with the following piece of code (change data values of the following fields, i. e., SERVER, PORT, DATABASE, USER and PASSWORD):

C#
string conStr = "DRIVER={MySQL ODBC 5.1 
Driver};SERVER=localhost;PORT=3306;DATABASE=RBAC;USER=root;PASSWORD=xxx;OPTION=3";
 //Connection string to RBAC database.
IDbConnection con = (IDbConnection)new OdbcConnection(conStr);
con.Open(); //Open the connection

Now you can proceed for initializing the nKnight datalayer by sending this kind of an open connection.

C#
DataLayer dbl = new DataLayer(con, DataLayer.DatabaseType.MySql); 
//Initialize datalayer by sending open connection and database type.

con here is a variable, change it with an actual connection string. User won't be able to understand what con is then. Or else, you can declare con in the previous line.

Remember: This release only accepts Mysql database.

After this, you will need to activate nKnight security layer by sending this datalayer object and user name and password. Confused!!! Ok don’t worry, I will show you how:

C#
bool d = SecurityPrincipal.InitSecuritySystem(dbl, "Admin", "Admin"); //Initializing security
 //system by sending datalayer object and username and password. 
 //If anything goes wrong, then it will throw an error 

Once you’ve set up security layer, then just drag and drop nKnight controls from toolbox into your forms. System will automatically generate one unique id for every control and you can access it from “GroupUniqueId” property.

3. It's An Open Source Project

I have introduced this nKnight SDK as an open source so that programmer can freely use this and can enrich the SDK. I know that without your valuable suggestions, this SDK will be stuck into 1.0.0, so please go ahead......

4. Download Link

So happy coding and help me to enrich this SDK...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)