473,385 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Article : Code Access Security Part - 2 (.Net FrameWork Tools Series)


Hey Guys,

Before we start with our sample app we need to view the security
configuration files on the machine. You will find them under

<drive>\WInNT\Microsoft.NET\FrameWork\<version>\Co nfig

Enterprise Level Security configuration file is :- enterprise.config

Machine Level Security configuration file is :- security.config

You will find the user security configuration file in

<drive>:\Documents and Settings\<userprofile>\Application

Data\Microsoft\CLR Security Config\v1.1.4322\security.config

Let us now create our sample app.In this we will create .Windows Forms
application which will try and read and write to the local disk.

1) Go to VS.NET create a new Win App.

2) On the Form Place one text box And one button Make the multiline property
of the text box true.

3) In the click event of the button write the followinf piece of code which
writes to a file wat ever is written in the text box.

StreamWriter sWriter = new StreamWriter("C://MyTextFile.txt");

sWriter.Write(textBox1.Text);

sWriter.Flush();

sWriter.Close();

4) If you run this from your machine you will be able to create the file and
write the textbox contents in it.

Well Currently this code is executing on the local machine cause in the
local mahinc policy MyComputer Zone has Full trust permission set.

Check it out by typing caspol -m -lg
Suppose if we were to run this same app from a local network share then the
Intranet code access group does not have the permission to write to the
local hard disk.

5) Place the exe on a network share and execut it. It should give you a
Security Permission Exception.

6) Modify your code to catch the exception and give a user friendly message.
Run the file again from the network share.

Suppose that we wanted this application to run from the network share. For
that we will need to change the Intranet Permission set.

caspol.exe -chggroup 1.2 FulTrust. // This command tells to fully trust all
the intranet applications

Note : Please be extremely careful to chagne the permission sets as this can
coz a lot viruses and other spy wares to come in. Change the permission sets
only if you have not made any custom changes to your PC. After changing the
permission set use

caspol.exe -reset command this resets the .NET default permission sets for
all code groups

Thus in this way we can prevent malicious code to access our resources.

Lets now explore the other options of caspol.exe

Turning the Security On/Off

It is possible to turn the .Net Security Off if so for any reason. By

default it is On.

caspol.exe -security off // to turn of the .Net security

To reset the security to .Net default security use

caspol.exe -reset

To create a new code group

caspol.exe -addgroup 1.3 -site www. <name of the site> /// this will add
full trust for any content from this site.

To create a code group under intranet with fulltrust to a particular share
on the network

caspol.exe -addgroup 1.2 -url file:///\\<machinename>/<foldername>/*
FullTrust

To remove a code group give the codegroup number (as shown in the list
groups) with -remgroup option

caspol.exe -remgroup 1.3.2

To change the code group's permission( we just sw above when we changed the
permission for our intranet code group)

caspol.exe -chggroup 1.2 FullTrust

You can add code group for a particular strong name E.g. If you have an
application MyApp.exe and you want any version of this application have
FullTrust you can achieve that by using the a similar command

caspol.exe -addgroup l -strong -file \bin\debug\MyApp.exe -
noname -noversion FullTrust

This command will a new strong Name code group. You can view it by giving
caspol -lg command.

You will see that are already 2 strong name code groups installed by
default. They belong to Microsoft and ECMA.

-- Please post your queries and comments for my articles in the usergroup
for the benefit of all. I hope this step from my end is helpful to all of
us.

Regards,

Namratha (Nasha)



Jul 21 '05 #1
1 2329
This was a great article Thanks

"Namratha Shah (Nasha)" wrote:

Hey Guys,

Before we start with our sample app we need to view the security
configuration files on the machine. You will find them under

<drive>\WInNT\Microsoft.NET\FrameWork\<version>\Co nfig

Enterprise Level Security configuration file is :- enterprise.config

Machine Level Security configuration file is :- security.config

You will find the user security configuration file in

<drive>:\Documents and Settings\<userprofile>\Application

Data\Microsoft\CLR Security Config\v1.1.4322\security.config

Let us now create our sample app.In this we will create .Windows Forms
application which will try and read and write to the local disk.

1) Go to VS.NET create a new Win App.

2) On the Form Place one text box And one button Make the multiline property
of the text box true.

3) In the click event of the button write the followinf piece of code which
writes to a file wat ever is written in the text box.

StreamWriter sWriter = new StreamWriter("C://MyTextFile.txt");

sWriter.Write(textBox1.Text);

sWriter.Flush();

sWriter.Close();

4) If you run this from your machine you will be able to create the file and
write the textbox contents in it.

Well Currently this code is executing on the local machine cause in the
local mahinc policy MyComputer Zone has Full trust permission set.

Check it out by typing caspol -m -lg
Suppose if we were to run this same app from a local network share then the
Intranet code access group does not have the permission to write to the
local hard disk.

5) Place the exe on a network share and execut it. It should give you a
Security Permission Exception.

6) Modify your code to catch the exception and give a user friendly message.
Run the file again from the network share.

Suppose that we wanted this application to run from the network share. For
that we will need to change the Intranet Permission set.

caspol.exe -chggroup 1.2 FulTrust. // This command tells to fully trust all
the intranet applications

Note : Please be extremely careful to chagne the permission sets as this can
coz a lot viruses and other spy wares to come in. Change the permission sets
only if you have not made any custom changes to your PC. After changing the
permission set use

caspol.exe -reset command this resets the .NET default permission sets for
all code groups

Thus in this way we can prevent malicious code to access our resources.

Lets now explore the other options of caspol.exe

Turning the Security On/Off

It is possible to turn the .Net Security Off if so for any reason. By

default it is On.

caspol.exe -security off // to turn of the .Net security

To reset the security to .Net default security use

caspol.exe -reset

To create a new code group

caspol.exe -addgroup 1.3 -site www. <name of the site> /// this will add
full trust for any content from this site.

To create a code group under intranet with fulltrust to a particular share
on the network

caspol.exe -addgroup 1.2 -url file:///\\<machinename>/<foldername>/*
FullTrust

To remove a code group give the codegroup number (as shown in the list
groups) with -remgroup option

caspol.exe -remgroup 1.3.2

To change the code group's permission( we just sw above when we changed the
permission for our intranet code group)

caspol.exe -chggroup 1.2 FullTrust

You can add code group for a particular strong name E.g. If you have an
application MyApp.exe and you want any version of this application have
FullTrust you can achieve that by using the a similar command

caspol.exe -addgroup l -strong -file \bin\debug\MyApp.exe -
noname -noversion FullTrust

This command will a new strong Name code group. You can view it by giving
caspol -lg command.

You will see that are already 2 strong name code groups installed by
default. They belong to Microsoft and ECMA.

-- Please post your queries and comments for my articles in the usergroup
for the benefit of all. I hope this step from my end is helpful to all of
us.

Regards,

Namratha (Nasha)



Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Smith | last post by:
I don't think I have understood the concept of Code Access Security in Dotnet fully. 1) I simply can't appreciate the method - *Permission.Assert that asserts the 'right' and bypasses the...
0
by: Brian Loesgen | last post by:
The next San Diego .Net User Group meeting is Tuesday, November 25, 2003 at the Scripps Ranch Library. Scripps Ranch Library 10301 Scripps Lake Drive San Diego, CA 92131-1026 Please join us...
1
by: Ramzey | last post by:
I've looked through the MSDN documentation on code access security and can not seem to find an answer to my question. I have a class XYZCorpWebPage that uses System.Web.UI.Page as it's base...
1
by: JDeats | last post by:
Does anyone know if it's possible to programmatically modify .NET 1.1 Configuration Code Access security? Our ideal is to have a MSI installer package that the user can run that will adjust the...
0
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to...
1
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Before we start with our sample app we need to view the security configuration files on the machine. You will find them under <drive>\WInNT\Microsoft.NET\FrameWork\<version>\Config ...
1
by: Jeremy S. | last post by:
..NET's code Access Security enables administrators to restrict the types of things that a .NET application can do on a local computer. For example, a ..NET Windows Forms application can be...
1
by: Vedo | last post by:
Hi, Is there a way to assign permissions to native applications in Windows? Like app x can only access this and this directories, app y cannot impersonate the remote users, etc. Something...
0
by: =?Utf-8?B?TWlrZSBNY0FsbGlzdGVy?= | last post by:
First, I'm not an AD Administrator. However, I have an important business need to be able to learn and pass along information about configuring .NET Code Access Security from Active Directory. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.