Check for permission does not work

  • Thread starter Thread starter Marius Groenendijk
  • Start date Start date
M

Marius Groenendijk

Hi group,

I want my app to show a msg if my it doesn't have the required
permission(s), however this simply doesn't work.

What am I overlooking/doing wrong??

[VB.NET]
Try
Dim x As New SecurityPermission(SecurityPermissionFlag.UnmanagedCode)
x.Flags = Security.Permissions.SecurityPermissionFlag.UnmanagedCode
x.Demand()
MessageBox.Show("YES UnmanagedCode permission")
Catch ex As Exception
MessageBox.Show("NO UnmanagedCode permission")
' application.exit
End Try

To ensure that my app doesn't have UnmanagedCode permission I run it from
a network share. But the app always claims to have unmanaged permission.
Trying a pinvoke (=unmanaged) for instance gives a security exception.

TIA,
Marius.
 
Marius,

Demands just check the callers on the stack, not the current method. If
you're running this from you application's Main method, you'll probably find
the result you expect if you move this code into a separate method that can
be called from Main.

HTH,
Nicole
 
You shouldn't need the x.Flags line. The following sample throws a security
exception when it demands the UnmanagedCode permission. If you comment out
the attribute, then the demand works. Hope this helps.

Imports System.Security.Permissions

Module Module1

<SecurityPermissionAttribute(SecurityAction.Deny, UnmanagedCode:=True)>
_
Sub Main()
Dim s As New
SecurityPermission(SecurityPermissionFlag.UnmanagedCode)
s.Demand()
End Sub

End Module

- Scott Swigart
blog: http://ea.3leaf.com
 
Thanks Nicole! That did it.


Nicole Calinoiu said:
Marius,

Demands just check the callers on the stack, not the current method. If
you're running this from you application's Main method, you'll probably
find the result you expect if you move this code into a separate method
that can be called from Main.

HTH,
Nicole


Marius Groenendijk said:
Hi group,

I want my app to show a msg if my it doesn't have the required
permission(s), however this simply doesn't work.

What am I overlooking/doing wrong??

[VB.NET]
Try
Dim x As New SecurityPermission(SecurityPermissionFlag.UnmanagedCode)
x.Flags = Security.Permissions.SecurityPermissionFlag.UnmanagedCode
x.Demand()
MessageBox.Show("YES UnmanagedCode permission")
Catch ex As Exception
MessageBox.Show("NO UnmanagedCode permission")
' application.exit
End Try

To ensure that my app doesn't have UnmanagedCode permission I run it from
a network share. But the app always claims to have unmanaged permission.
Trying a pinvoke (=unmanaged) for instance gives a security exception.

TIA,
Marius.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top