Thursday, December 16, 2010

Code Access Security


In VS2010 to do CAS we sholud create the following App.Config file

then we try to block the read or write permissions in a particular file or folder

using System.Security.Permissions; // Add reference
using System.IO;
[assembly: FileIOPermission(SecurityAction.RequestRefuse,Read = @"C:\")]
namespace CASs
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void btnread_Click(object sender, EventArgs e)
{
try
{
StreamReader sr = new StreamReader(@"C:\sunil2.txt");
textBox1.Text = sr.ReadToEnd();
sr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

}
}

we cannot read the file since we set the security rules

0 comments:

Post a Comment