Groups | Blog | Home
all groups > dotnet security > october 2004 >

dotnet security : ArgumentException with MsgBox when working with code access security


Gabriel Michaud
10/9/2004 10:31:22 PM
Hello,

Can anyone explain to me why I get an ArgumentException on the MsgBox with
the following code? I'm studying for the 70-306 exam and I'm experimenting
with code access security.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim perm As New
System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.Read,
"C:\dell.sdr")
perm.PermitOnly()
Dim file As New System.IO.FileInfo("C:\dell.sdr")
MsgBox("Done!")
End Sub

The problem does not happen if I do a RevertPermitOnly before doing the
MsgBox.

Thank you

Nicole Calinoiu
10/10/2004 7:57:05 AM
Gabriel,

When you call the MsgBox method without specifying the title parameter, the
method will attempt to use the caller's assembly name for the message box
title. To determine the name of the calling assembly,
FileIOPermission.PathDiscovery for the assembly path is required, and your
PermitOnly blocks it since the assembly is not at c:\dell.sdr. The MsgBox
code also suppresses the actual exception received while attempting to read
the assembly name, substituting the ArgumentException you received in its
place.

Even if you were to specify your own title (or call
System.Windows.Forms.MessageBox.Show to avoid the title inference), you
would still have an additional problem. Both MsgBox and MessageBox.Show
require UIPermission for safe sub-windows, so you'll need to add this to a
permit-only permission set in order to display the message box. e.g.:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim permSet As New PermissionSet(PermissionState.None)
permSet.AddPermission(New
System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.Read,
"C:\dell.sdr"))
permSet.AddPermission(New
UIPermission(UIPermissionWindow.SafeSubWindows))
permSet.PermitOnly()

Dim file As New System.IO.FileInfo("C:\dell.sdr")
MsgBox("Done!", , "Some title")

CodeAccessPermission.RevertAll()

End Sub

HTH,
Nicole


[quoted text, click to view]

AddThis Social Bookmark Button