Re: Checking security at startup?

From: Allen (agj@bigfoot.com)
Date: 12/11/02


From: "Allen" <agj@bigfoot.com>
Date: Wed, 11 Dec 2002 01:07:13 -0000


Hello Peter,

Firstly I think the strange behavior you are seeing is a result of the way
the stack walk initiated by the Demand() works. The Demand actually ignores
the permissions assigned to the method that calls it, and looks first at the
immediate caller of the method. Given that the runtime called your Main
method, this is probably the source of your problem, and will never result
in the behavior you want.

The way to do what you want is to use a minimum permission request. This
causes your assembly to fail to load if the runtime cannot grant you the
permission you need to run.

Stick the following lines in your assembly:

using System.Data.SqlClient;
using System.Security.Permissions;

[assembly:SqlClientPermission(SecurityAction.RequestMinimum,
Unrestricted=true)]

Alternatively, if you want to test in your code whether you have a
permission, use the SecurityManager.IsGranted() method. However, the minimum
pemission request is a better approach because it is easier for people to
see the permissions that your code requires to run.

Regards
Allen

"Peter Erikson" <peter.erikson@toolbar.se> wrote in message
news:094101c2a0a0$ca04f810$8af82ecf@TK2MSFTNGXA03...
> Hello,
>
> I started a thread a couple of days ago in the Windows
> Forms newsgroup with the title 'Catching unhandled
> Exceptions', and i am beginning to think that the problem
> i am aboute to describe here has a similar origin.
>
> The thing is... I would like to check if the application
> i am making have permissions to use SqlClient
> functionality at startup. So i am doing the following in
> my Main method...
>
> [STAThread]
> static void Main()
> {
> try
> {
> SqlClientPermission pSqlClient = new
> SqlClientPermission();
> pSqlClient.Demand();
> Application.Run(new App());
> }
> catch(System.Security.SecurityException x)
> {
> MessageBox.Show(x.Message);
> }
> }
>
> The problem is, if i dont have SqlClien permissions, the
> SecurityException never is caught. Insted there is a
> Framework dialog telling me that there has been a
> SecurityException. The SecurityException somehow got
> passed the catch block?? Does anybodey know why this is
> happening?
>
> ...to me it looks like there is some kind of strange
> behavioure when doing Exception handeling and the Main
> method..
>
> any thoughts??
>
> thanx
>
> /Peter Erikson



Relevant Pages

  • Re: Checking security at startup?
    ... And as i said earlier the exception ... the actual code that generates the SecurityException (if ... SqlClientPermission p = new SqlClientPermission; ... but if I for instance test if i have permissions to write ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Problem with security settings for internet explorer
    ... >> Sherlock Holmes ... > was pretty good with 98se but never ntfs or permissions and am trying ... > Admin account and I can right-click on the desktop icon and go to ... I think that is the only strange behavior that ...
    (microsoft.public.windowsxp.security_admin)
  • Re: [AppArmor 39/45] AppArmor: Profile loading and manipulation, pathname matching
    ... only if you use the most restrictive permissions. ... and for which there is demand. ... SELinux is superior to AA for a certain scenario of use cases; ... SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 ...
    (Linux-Kernel)
  • Re: SecurityException on GetTempFileName()
    ... I am guessing it's the assembly permissions in the client library (which I ... > permission for the demand to pass. ... If any assembly on the call stack lacks ... >> communication DLL does not need any of the above permissions. ...
    (microsoft.public.dotnet.framework)
  • Re: SecurityException on GetTempFileName()
    ... > I am guessing it's the assembly permissions in the client library (which I ... >> permission for the demand to pass. ... If any assembly on the call stack ... >> description of how assembly-level permissions attributes work, ...
    (microsoft.public.dotnet.framework)