all groups > dotnet security > september 2004 >
You're in the

dotnet security

group:

.Net Code Access Imperative Security


.Net Code Access Imperative Security Sameeksha
9/9/2004 3:01:02 AM
dotnet security:
Hello All,

I wrote the following code in a button click event in a windows application.

CodeAccessPermission perm = new
FileDialogPermission(FileDialogPermissionAccess.Open);
perm.Deny();
try
{
OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.ShowDialog();
}
catch (SecurityException ex)
{
MessageBox.Show("You are not authorized to open file dialog");
MessageBox.Show(ex.Message);
}

Output that I expect is the exception message boxes. But instead it shows
the open file dialog box and does not throw any exception. Why should it be
able to open file dialog even when permission is explicitly denied?
(perm.Deny())

Thanks in advance
Sameeksha
--
Sameeksha,
Re: .Net Code Access Imperative Security Sameeksha
9/9/2004 5:21:06 AM
Hello,

Thanks a lot. When I tried opening file chosen in the open file dialog, it
did give security exception.

I've one more similar piece of code with similar problem. It is as follows:

CodeAccessPermission perm = new
FileIOPermission(FileIOPermissionAccess.Write,"C:\\");
try
{
perm.Deny();
StreamWriter sw = new StreamWriter("C:\\myfile.txt",true);
sw.WriteLine("***************************************");
sw.Close();
}
catch (SecurityException ex)
{
MessageBox.Show(ex.Message);
}
This code ALWAYS writes to the specified file without any SecurityException.
What could be the reason?

Thanks in advance
Sameeksha

[quoted text, click to view]
Re: .Net Code Access Imperative Security Nicole Calinoiu
9/9/2004 7:41:53 AM
Sameeksha,

The OpenFileDialog class only demands the FileDialogPermission when its
OpenFile method is called, not when the dialog is displayed. Doesn't
necessarily make much sense, but that's the the way it's implemented...
That said, you can get your desired result by denying UIPermission, but
that's not necessarily worthwhile since denials can be overridden by
assertions on the same call stack.

HTH,
Nicole


[quoted text, click to view]

Re: .Net Code Access Imperative Security Nicole Calinoiu
9/9/2004 8:43:40 AM
If you want to block appending to an existing file, use
FileIOPermissionAccess.Append, not FileIOPermissionAccess.Write. You can,
of course, use both. e.g.:

CodeAccessPermission perm = new
FileIOPermission(FileIOPermissionAccess.Write |
FileIOPermissionAccess.Append ,"C:\\");

HTH,
Nicole


[quoted text, click to view]

Re: .Net Code Access Imperative Security Sameeksha
9/9/2004 8:53:02 PM
Great!!!! It did work. Thanks a lot.

[quoted text, click to view]
AddThis Social Bookmark Button