Understanding Code Access Security in .NET
page 8 of 12
by Joydip Kanjilal
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 51536/ 187

Using Code Access Security in .NET

In this section we will explore how we can use CAS from our applications programmatically. Here is a sample code.

Listing 1

private void btnConnectDB_Click(object sender, EventArgs e)
  DataSet dsContact = new DataSet();
  SqlConnection dbConnection = new SqlConnection(
  SqlCommand dbCommand = new SqlCommand("SELECT * FROM Person.Contact",
  SqlDataAdapter dataAdapter = new SqlDataAdapter(dbCommand);
  MessageBox.Show(String.Format("{0} Rows fetched from the database.",
private void btnReadFile_Click(object sender, EventArgs e)
  StreamReader sr = File.OpenText(@"D:\Sample.txt");
  string content = sr.ReadToEnd();
private void btnWriteFile_Click(object sender, EventArgs e)
  StreamWriter sw = new StreamWriter(@"D:\Sample.txt");
  sw.WriteLine("Hello World!");

Refer to the code snippet given above. What we are trying to do is an attempt to connect to the database in the click event of the button btnConnectDB, and then we are trying to read and write in the subsequent buttons. Now you need to compile this project with a strong name and then configure the security permissions using the .NET Configuration Tool. In the section that follows we will explore how we can use the .NET Configuration Tool to configure security permissions.

View Entire Article

User Comments

No comments posted yet.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2020 ASPAlliance.com  |  Page Processed at 2020-07-14 11:40:36 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search