C#
  Home arrow C# arrow Page 4 - Implementing Role Based Security using C#
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Implementing Role Based Security using C#
By: Victor Stachura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 17
    2006-03-29

    Table of Contents:
  • Implementing Role Based Security using C#
  • Declarative and Imperative Security
  • Now you have a WindowsPrincipal Object. So what?
  • Design Principles to Implement Role Based Security

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Implementing Role Based Security using C# - Design Principles to Implement Role Based Security


    (Page 4 of 4 )

    In this article I covered three different ways to access code based on a user’s membership in a group.  These ways were 1) WindowsPrincipal.IsInRole(),  2) using declarative demands and 3) using imperative security.  Each method serves a purpose and should be used in specific scenarios.

    If you need to restrict access to an entire method, you should use Declarative security. If you need to restrict access to an entire method or portions of a method, you should use Imperative security because it gives you finer granularity. If you need to perform different actions based on a user's role, or you need to restrict portions of code, you should use WindowsPrincipal.IsInRole().

    There is one more point to consider: maintenance.  Declarative security is slightly more secure because it protects you when changes are made to the code in the future.  Since the scope is for the entire method, any changes to the method will continue to be restricted by the security declarations.  If you’re using imperative security you run the risk of code changes being made in sections of the method where unauthorized users could execute code.  This typically occurs when changes are made to code outside of a try block.

    The GenericPrincipal and GenericIdentity Classes

    There is one more set of classes that need to be mentioned – the GenericPrincipal and GenericIdentity classes.  These classes are based on the IIdentity and iPrincipal interfaces and implement only the basic functionality of those interfaces.  GenericPrincipal and GenericIdentity are used to create custom identities and attach them to the current thread (remember that each thread runs as a user with its own security context).

    The primary use of these classes is to assign group membership to users when you are using Forms Authentication, and not relying on users to have Windows domain accounts (typically used on Web applications where your user base is people outside of your company).  In this scenario you have user ids and passwords stored in a database.  When users log in they authenticate with the data in the database.  But this does not give your running application the security information it needs for these users.  To get around this problem you create an identity object for the user with the GenericIdentity class and specify the groups to which the user belongs.  The next step is to create a GenericPrincipal object and assign the user’s access rights to the current thread.  Then the rest of your application can execute using the security context you established for the user.

    Conclusion

    In this article I covered the basics of implementing Role Based Security (RBS) in your applications.  RBS is the ability to limit application access based on a user’s role and is an example of applying the principle of Defense in Depth – implementing multiple levels of security to provide the maximum amount of protection to your application.  During design you’ll have to decide which method you’re going to use to implement RBS (WindowsPrinciple, Declarative or Imperative).   The GenericPrincipal class is used to associate identity rights to users when Forms authentication is used.  Whatever your scenario, the .NET framework has the tools you need to secure your applications.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This article was a great eyeopener for me. Thx.But .. I have a case where I need...
     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT