|
User can also create their own permission sets and code groups in the C#. The following are the steps needs to follow to create your own permission sets and code groups:-
Steps to create your own permission set and Code group
- Go to Control Panel --> Administrative Tools --> Microsoft .NET Framework 2.0 Configuration à Double click on this a popup windows appear

2. Creating a new Permission Set à Expand “Runtime Security Policy”. There are 3 security policy levels i.e. Enterprise , Machine and User. We are creating the permission set for the machine security level.

3. Assign the permission to the permission set: Each permission set is a collection of much different permission to various resources on the computer. Select the permission that you need in the permission set.

4. Here I am selecting the “File IO” permission from the pool of permission set. And then change the settings for this permission. Here specify the “C:\” in the file path and assign only reading permission to this.

5. Here we assign 3 permission to the newly create permission set i.e. File IO, Security and User Interface.

Create New Code Group
Create new code group and assign the permission that you create to this code group so that your assembly will work under the specified permission set. Right click on the All_Code node and this will open a pop-up window i.e. Create Code Group wizard. Specify the name for your code group.

1. Choose a condition type for the code group:
The membership condition determines whether or not an assembly meets specific requirements to get the permission associated with a code group.
Condition type in the specified context: Strong Name. You have to just click on the Import button à Select the assembly from the pop-up and this will extract the information that we needed.

2. Assign a Permission Set to the code group:
Here through this window we can assign the permission set to this code group that we are going to create. Select the permission set that we created earlier. Though this window either we can create new permission set or reuse the existing one.

3. Go through the properties of the newly created Code Group
Right click on the code group that you just created and you can view the “Code Group ” properties like the Code Group Name, its membership conditions and permission set that is associated to it.

Create an Application: That will perform read and write operations on file.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace winCreatingPSET
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click( object sender, EventArgs e)
{
StreamWriter _streamWriter = new StreamWriter ( "c:\\TestPermission.txt" );
_streamWriter.WriteLine( " Security is Important" );
_streamWriter.WriteLine( " Security can provide Authentication " );
_streamWriter.Close();
}
private void button1_Click( object sender, EventArgs e)
{
StreamReader _streamWriter = new StreamReader ( "c:\\TestPermission.txt" );
lstPSET.Items.Add(_streamWriter.ReadLine());
lstPSET.Items.Add(_streamWriter.ReadLine());
_streamWriter.Close();
}
}
}
Output:
Now run the application and try to click on the “write” button the screen, this will give you following exception.:-

|