VisualBuilder
  Home > Csharp > Tutorials > Assembly Information : Getting Permission set of the assembly - C# Tutorial
Tell a friend
Link to us
Total Members
      Members: 84606
     
Sitemap Forum Chat
Home
C# Tutorial Home
1 . Introduction to C sharp
2 . Control Statement: Selection Statement in C sharp
3 . Control Statements – Loops Statement
4 . Methods
5 . Namespaces
6 . Introduction to Classes:
7 . Inheritance:
8 . Polymorphism
9 . Properties
10 . Indexers
11 . Structs
12 . Interfaces
13 . Delegates:
14 . Exception Handling:
15 . Attributes
16 . Enums
17 . Encapsulation
18 . Parameter Passing in C sharp
19 . Method Overloading
20 . Database Interaction Using C sharp
21 . Operator Overloading in C sharp -1
22 . Operator Overloading in C sharp -2
23 . Operator Overloading in C sharp -2
24 . Sockets
25 . DNS [Domain Name System]
26 . Working with Files
27 . Generating Help File in C sharp
28 . Code Access Security
29 . Multi-Threading
30 . Globalization and Localization -1
31 . Working with Registry in C sharp
32 . Globalization and Localization -2
33 . Windows Service
34 . Web Service
35 . Consuming Web Services
36 . Creating Proxy Object of Web Service
37 . Creating an XML document
38 . Reading XML document in C sharp
39 . Using XMLWriter class to Write XML document in C sharp
40 . Assembly Information : Getting Permission set of the assembly
41 . Creating your own Permission Set
42 . Using C sharp Socket
 
Csharp Group Home
Csharp Discussion (7)
Csharp Members (2250)
Csharp Resources
Csharp Source Code (35)
Csharp Articles (0)
Csharp Blogs
Csharp Jobs
Csharp Components (5)
Csharp Books
Csharp Websites (25)
Csharp News (17)
Csharp Q & A (4)
- Csharp Ask Question
- Csharp Questions
- Csharp Unanswered Questions
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (432 )
Source Code (3217 )
Articles (11 )
Components (1589 )
News (880 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise


Csharp Tutorial
 Assembly Information : Getting Permission set of the assembly
  << Prev: Using XMLWriter class to Write XML document in C sharp Next: Creating your own Permission Set >>

The basics of CAS is whenever any code is being executed in managed world the .NET runtime verifies whether that code is allowed or not based on evidence and set of permissions. Two important things that are very much importance to the framework is:



  • Evidence:- From where the code comes? Is the code managed or unmanaged.

  • Permissions:- The permission set on which the code executes.


 


 


Permissions and Permission Sets


Permission is what a code can do with particular resource like File, Registry etc., and Permission Set is collection of permission.


 


Policy Levels


NET System comes up with 4 Policies that are Enterprise , Machine User, and AppDomain (which can be done through programmatically). Each policy has multiple code groups and multiple permission sets.They have the hierarchy given below.


 




Enterprise : All managed code in an enterprise setting.


Machine: All managed code on the computer.


User: Code in all processes associated with the current user.


Application Domain: Managed code in the host's application domain.


 


Example:- To get the permission set of Current Assembly.


 


Form13.cs


 


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


using System.Threading;


using System.Diagnostics;


using System.Reflection;


using System.Security;


using System.Security.Policy;


using System.Security.Permissions;


using System.Collections;


 


namespace _CSharpApplication


{


  public partial class Form13 : Form


    {


    //NAME OF THE BUILD_IN NAMED PERMISSION SET FOR FULLTRUST.


    const string sFullTrust = "FullTrust" ;


    static PermissionSet finalSet = new NamedPermissionSet ( "FinalAssemblySet" );


    static PermissionSet permSet = null ;


    //FIND OUT WHETHER THIS ASSEMBLY IS FULLTRUST PERMISSIONS.


    static bool fullTrust = true ;


    public Form13()


        {


    InitializeComponent();


        }


    private void Form13_Load( object sender, EventArgs e)


        {


        }


    //FIGURE OUT THE CODEGROUP AND THE POLICY LEVEL OF THE ASSEMBLY.


    static bool isResGroups( CodeGroup _codeGroupparent, PolicyLevel _policyLevel)


        {


            NamedPermissionSet _namedPermissionSet = _policyLevel.GetNamedPermissionSet(     _codeGroupparent.PermissionSetName);


      if (isFullTrust(_namedPermissionSet)) return true ;


      if (permSet == null ) permSet = ( PermissionSet )_namedPermissionSet;


      else permSet = permSet.Union(_namedPermissionSet);


      if (_codeGroupparent.Children.Count > 0)


            {


        foreach ( CodeGroup cp in _codeGroupparent.Children)


                {


            if (cp.Children.Count > 0)


                            isResGroups(cp, _policyLevel);


            else


                        {


                            NamedPermissionSet nps2 = _policyLevel.GetNamedPermissionSet( cp.PermissionSetName);


               if (isFullTrust(nps2))


                return true ;


                              permSet = permSet.Union(nps2);


                        }


                }


            }


    //FULL TRUST CODE NOT FOUND


    return false ;


    }


    //CHECK WHETHER THE PERMISSION SET IF FULLTRUST OR NOT FOR THE CURRENT ASSEMBLY.


    static bool isFullTrust( NamedPermissionSet _namedPermissionSet)


        {


        if (_namedPermissionSet.Name.Equals( "FullTrust" ))


            return true ;


        return false ;


        }


    //PASS THE PERMISSION SET AND LISTBOX AS ARGUMENT TO THE FUNCTION.


    static void getOutput( PermissionSet _permissionSet, ListBox _listBox)


        {


            IEnumerator psEnumerator = _permissionSet.GetEnumerator();


      while (psEnumerator.MoveNext())


                _listBox.Items.Add(psEnumerator.Current);


        }


    private void button1_Click( object sender, EventArgs e)


        { //Fetching the Permission Set of the Assembly


        lstPermission.Items.Add( "List of permissions assign to current assembly" );


        IEnumerator policy = SecurityManager .PolicyHierarchy();


    while (policy.MoveNext())


        {


            PolicyLevel currentLevel = ( PolicyLevel )policy.Current;


            CodeGroup group = currentLevel.ResolveMatchingCodeGroups ( Assembly .GetExecutingAssembly().Evidence);


            fullTrust &= isResGroups(group, currentLevel);


      if (!fullTrust)


            {


        if (finalSet == null ) finalSet = permSet;


        else finalSet = finalSet.Intersect(permSet);


                permSet = null ;


            }


        else


            {


                lstPermission.Items.Add( "Current Level-" + currentLevel.Label + " || " + "Group--" +     group.Name + " || " + "Group Policy--" + group.PermissionSetName);


            }


        }


    if (fullTrust)


            lblMode.Text = "Assembly is running in full-trust mode." ;


    else


            getOutput(finalSet, lstPermission);


        }


    }


}


 


 


Output:


 




 


Clicking on the “Assembly Information” button.


 



  << Prev: Using XMLWriter class to Write XML document in C sharp Next: Creating your own Permission Set >>
Csharp Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
LINKS
MSN
Video Surveillance
Skype vs. sipcall
Gift to Pakistan
 
ADVERTISEMENT
 
PARTNER LIST

More
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising