Re: Checking security at startup?
From: Allen (agj@bigfoot.com)
Date: 12/11/02
- Next message: Mike Moore [MS]: "Re: SignOut a User"
- Previous message: Peter Erikson: "Checking security at startup?"
- In reply to: Peter Erikson: "Checking security at startup?"
- Next in thread: Peter Erikson: "Re: Checking security at startup?"
- Reply: Peter Erikson: "Re: Checking security at startup?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
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
- Next message: Mike Moore [MS]: "Re: SignOut a User"
- Previous message: Peter Erikson: "Checking security at startup?"
- In reply to: Peter Erikson: "Checking security at startup?"
- Next in thread: Peter Erikson: "Re: Checking security at startup?"
- Reply: Peter Erikson: "Re: Checking security at startup?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|