C#
  Home arrow C# arrow Page 3 - 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# - Now you have a WindowsPrincipal Object. So what?


    (Page 3 of 4 )

    The first thing you want to do is verify that the user is authenticated. Prior to accessing your application, the user should have entered a userid and password and been allowed into the system.

    if (curIdentity.IsAuthenticated)
    {
    // User is authenticated, proceed
    }
    else
    {
    // Not authenticated, DO NOT allow access
    }

    Once you’re satisfied that a user is authenticated, you can now verify they belong to a role that is allowed to access a section of code and/or a particular feature.  The work horse method you’ll use for this is called IsInRole().   To use IsInRole() you can pass in a Windows built-in role or a specific role that you may have created (a custom role or group).  This is how you can restrict access to sections of code.

    if (myPrincipal.IsInRole( @"myCompany\Employee_Role”)
    {
        // put code here
    }
    else
    {
        // User is not allowed to access code above
        // do what you want here
    }

    Built in Role Member

    Group

    AccountOperator

    Account Operators

    Administrator

    Administrators

    BackupOperator

    Backup Operators

    Guest

    Guests

    PowerUser

    Power Users

    PrintOperator

    Print Operators

    Replicator

    Replicator

    SystemOperator

    Server Operators

    User

    Users

    You can use the Built in Roles like this:

    If (myPrincipal.IsInRole(  WindowsBuiltInRole.PowerUser )
          // Then do something useful here

    The use of Windows Built in Roles will vary from machine to machine.  If you query for a role and it doesn’t exist, an exception will be thrown.  Be prepared to catch the exception.

    The PrincipalPermission Class

    The PrincipalPermission Class enables you to demand that users of your code have been authenticated and/or belong to a specific role or group.  The Declarative syntax is used and the security context is checked before a method is executed.  You also can supply multiple security declarations for a method.

    There are three properties that you MUST know:

    1. Authenticated - If true the caller must be authenticated.
    2. Name - The user's user name must match this string.
    3. Role - The user must at least be a member of this role. 

    In the example below the user needs to be either a member of the Sr_Managers or Managers group to execute the method:

    [PrincipalPermissionAttribute(SecurityAction.Demand,
                            Name=@"myCompany\Sr_Managers ")][PrincipalPermissionAttribute(SecurityAction.Demand, 
                            Name=@"myCompany\Managers ")]
    Private void ManagersOnly()
    {
          //Do something for managers only
    }

    Remember, only one of the security declarations needs to be true in order for the method to execute.

    More C# Articles
    More By Victor Stachura


       · 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 1 hosted by Hostway
    Stay green...Green IT