Home All Groups Group Topic Archive Search About
Author
31 Mar 2005 12:00 PM
Feldman Alex
Hi all,



I need to know the user privileges (does user have administrator privileges)
..

Which  c# api's should i use?

Thanks a lot

Author
31 Mar 2005 4:39 PM
Joseph MCAD
March 31, 2005

    Assuming you are developing a Windows application, you can examine the
roles a user is in by using the System.Security.WindowsPrincipal class. (You
can also do this in a web application but it requires windows authentication
and impersonation.) This class provides the IsInRole method which accepts a
string or WindowsBuiltInRole as an argument and returns a boolean value
whether the user is in the requested role. It is used in this way:

   'You have to obtain the WindowPrincipal object     

   dim Prin as WindowsPrincipal = Threading.Thread.CurrentPrincipal
   if Prin.IsInRole(WindowsBuiltInRole.Administrator) = true then

     ' Perform tasks that should only be done if the user is an Admin

   else

     ' Perform limited or no tasks

   end if

   You can find more about this class on MSDN. Hope this helps and have a
great day!


    Joseph MCAD



Show quoteHide quote
"Feldman Alex" wrote:

> Hi all,
>
>
>
> I need to know the user privileges (does user have administrator privileges)
> ..
>
> Which  c# api's should i use?
>
> Thanks a lot
>
>
>
Author
31 Mar 2005 4:51 PM
Joseph MCAD
March 31, 2005

     The namespace the WindowsPrincipal class is in is
System.Security.Principal. I made a mistake in my last post. Best of luck!


            Joseph MCAD


Show quoteHide quote
"Joseph MCAD" wrote:

>
>    March 31, 2005
>
>     Assuming you are developing a Windows application, you can examine the
> roles a user is in by using the System.Security.WindowsPrincipal class. (You
> can also do this in a web application but it requires windows authentication
> and impersonation.) This class provides the IsInRole method which accepts a
> string or WindowsBuiltInRole as an argument and returns a boolean value
> whether the user is in the requested role. It is used in this way:
>
>    'You have to obtain the WindowPrincipal object     
>
>    dim Prin as WindowsPrincipal = Threading.Thread.CurrentPrincipal
>    if Prin.IsInRole(WindowsBuiltInRole.Administrator) = true then
>   
>      ' Perform tasks that should only be done if the user is an Admin
>
>    else
>   
>      ' Perform limited or no tasks
>
>    end if
>
>    You can find more about this class on MSDN. Hope this helps and have a
> great day!
>
>                                                                             
>     Joseph MCAD
>
>
>
> "Feldman Alex" wrote:
>
> > Hi all,
> >
> >
> >
> > I need to know the user privileges (does user have administrator privileges)
> > ..
> >
> > Which  c# api's should i use?
> >
> > Thanks a lot
> >
> >
> >
Author
31 Mar 2005 5:03 PM
Joseph MCAD
March 31, 2005

     Sorry, but before you can request certain roles, you first must assign
the currently logon on user (according to the Windows OS for windows
applications) to the threading.thread.currentprincipal by using this line of
code before all others:

appdomain.currentdomain.setprincipalpolicy(principalpolicy.windowsprincipal)

If you want to use role demands everywhere in code, then you might want to
put this line in the class's contructor or the form load event. Sorry, but it
will work now.


Joseph MCAD
Show quoteHide quote
"Joseph MCAD" wrote:

>
>    March 31, 2005
>
>     Assuming you are developing a Windows application, you can examine the
> roles a user is in by using the System.Security.WindowsPrincipal class. (You
> can also do this in a web application but it requires windows authentication
> and impersonation.) This class provides the IsInRole method which accepts a
> string or WindowsBuiltInRole as an argument and returns a boolean value
> whether the user is in the requested role. It is used in this way:
>
>    'You have to obtain the WindowPrincipal object     
>
>    dim Prin as WindowsPrincipal = Threading.Thread.CurrentPrincipal
>    if Prin.IsInRole(WindowsBuiltInRole.Administrator) = true then
>   
>      ' Perform tasks that should only be done if the user is an Admin
>
>    else
>   
>      ' Perform limited or no tasks
>
>    end if
>
>    You can find more about this class on MSDN. Hope this helps and have a
> great day!
>
>                                                                             
>     Joseph MCAD
>
>
>
> "Feldman Alex" wrote:
>
> > Hi all,
> >
> >
> >
> > I need to know the user privileges (does user have administrator privileges)
> > ..
> >
> > Which  c# api's should i use?
> >
> > Thanks a lot
> >
> >
> >