Checking if Administrator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • henrycortezwu@gmail.com

    Checking if Administrator

    Hi All,

    Quick question, I manage to come up with a working code the
    successfully checks if the current users is part of the Administrators
    group or not. My question is, will this code work from Windows 95? 98?
    ME? 2000?

    My development machine is currently XP, and it works fine here.

    Thanks in advance,
    Henry :)

    Private Function CheckAdministra torRole() As Boolean
    On Error Resume Next


    System.AppDomai n.CurrentDomain .SetPrincipalPo licy(Security.P rincipal.Princi palPolicy.Windo wsPrincipal)
    Dim prpAdministrato r As New
    System.Security .Permissions.Pr incipalPermissi on(Environment. UserName,
    "BUILTIN\Admini strators")

    prpAdministrato r.Demand()

    If Err.Number <> 0 Then
    CheckAdministra torRole = True
    Else
    CheckAdministra torRole = False
    End If
    End Function

  • henrycortezwu@gmail.com

    #2
    Re: Checking if Administrator

    Oh by the way, which is the better code, the 1st one i posted or the
    ff:

    Private Function CheckAdminRole( ) As Boolean
    Dim myDomain As AppDomain = System.Threadin g.Thread.GetDom ain()


    myDomain.SetPri ncipalPolicy(Se curity.Principa l.PrincipalPoli cy.WindowsPrinc ipal)
    Dim myPrincipal As Security.Princi pal.WindowsPrin cipal =
    CType(System.Th reading.Thread. CurrentPrincipa l,
    Security.Princi pal.WindowsPrin cipal)

    If myPrincipal.IsI nRole("BUILTIN\ Administrators" ) = True Then
    CheckAdminRole = True
    Else
    CheckAdminRole = False
    End If
    End Function

    It works the same, but what about scenarios on a user belonging to two
    or more domains? Which is the better code?
    And lastly, on my 1st code, what does the
    "System.AppDoma in.CurrentDomai n­.SetPrincipal Policy(Security .Principal.Prin cipalPolicy.Win do­wsPrincipal) "
    do? When I commented it out, the code still works.

    Thanks again,
    Henry :)

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Checking if Administrator

      Henry,

      Surely not on Windows'95 when you use VBNet, I never heard that it was not
      working on W98/me and the others. However why would it not, it is a dotNet
      part.

      The only restriction I see here.


      However I did not test it so I can be wrong.

      Cor






      Comment

      • Brian Cryer

        #4
        Re: Checking if Administrator

        <henrycortezwu@ gmail.com> wrote in message
        news:1122387573 .525557.10700@g 47g2000cwa.goog legroups.com...[color=blue]
        > Hi All,
        >
        > Quick question, I manage to come up with a working code the
        > successfully checks if the current users is part of the Administrators
        > group or not. My question is, will this code work from Windows 95? 98?
        > ME? 2000?
        >
        > My development machine is currently XP, and it works fine here.
        >
        > Thanks in advance,
        > Henry :)
        >[/color]

        As I recall, Windows 95, 98 and ME don't have the same security model as
        NT4/2000/XP and don't have the concept of an administrator (nor of a
        domain).

        Your code should therefore work on NT4 and 2000. I haven't a clue what it
        will do on 95/98/ME (not having tried it), but its possible it will degrade
        gracefully ...? Hopefully someone with 95/98/ME will try it for you.

        Brian.

        Brian Cryer's home page, a collection of notes on my professional and personal interests. Including C#, VB.Net, Windows, DB admin, Delphi and more.



        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Checking if Administrator

          <henrycortezwu@ gmail.com> schrieb:[color=blue]
          > Quick question, I manage to come up with a working code the
          > successfully checks if the current users is part of the Administrators
          > group or not. My question is, will this code work from Windows 95? 98?
          > ME? 2000?
          >[...]
          > Private Function CheckAdministra torRole() As Boolean
          > On Error Resume Next
          >
          >
          > System.AppDomai n.CurrentDomain .SetPrincipalPo licy(Security.P rincipal.Princi palPolicy.Windo wsPrincipal)
          > Dim prpAdministrato r As New
          > System.Security .Permissions.Pr incipalPermissi on(Environment. UserName,
          > "BUILTIN\Admini strators")
          >
          > prpAdministrato r.Demand()
          >
          > If Err.Number <> 0 Then
          > CheckAdministra torRole = True
          > Else
          > CheckAdministra torRole = False
          > End If
          > End Function
          >[/color]

          MSDN on 'IPrincipal.IsI nRole' and 'AppDomain.SetP rincipalPolicy' says:

          | *Requirements*
          |
          | *Platforms:* Windows 98, Windows NT 4.0, Windows
          | Millennium Edition, Windows 2000, Windows XP Home Edition,
          | Windows XP Professional, Windows Server 2003 family

          This means that the methods are supported by all versions of Windows running
          the .NET Framework. The .NET Framework doesn't work together with Windows
          95. I suggest to check the documentation for the requirements of other
          methods you are using too.

          Simpler solution for checking if the user is an administrator:

          \\\
          Imports System.Security .Principal
          ..
          ..
          ..
          Dim wp As New WindowsPrincipa l(WindowsIdenti ty.GetCurrent() )
          Dim IsAdmin As Boolean = wp.IsInRole(Win dowsBuiltInRole .Administrator)
          ///

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          • Cor Ligthert [MVP]

            #6
            Re: Checking if Administrator

            Herfried,

            I forgot to look at the bottom, good that you did that

            :-)

            Cor


            Comment

            • henrycortezwu@gmail.com

              #7
              Re: Checking if Administrator

              Thanks guys!

              98 is ok to me!! :) Clients are up to 98!

              Thanks again for the shorter code!!

              Henry :)

              Herfried K. Wagner [MVP] wrote:[color=blue]
              > <henrycortezwu@ gmail.com> schrieb:[color=green]
              > > Quick question, I manage to come up with a working code the
              > > successfully checks if the current users is part of the Administrators
              > > group or not. My question is, will this code work from Windows 95? 98?
              > > ME? 2000?
              > >[...]
              > > Private Function CheckAdministra torRole() As Boolean
              > > On Error Resume Next
              > >
              > >
              > > System.AppDomai n.CurrentDomain .SetPrincipalPo licy(Security.P rincipal.Princi palPolicy.Windo wsPrincipal)
              > > Dim prpAdministrato r As New
              > > System.Security .Permissions.Pr incipalPermissi on(Environment. UserName,
              > > "BUILTIN\Admini strators")
              > >
              > > prpAdministrato r.Demand()
              > >
              > > If Err.Number <> 0 Then
              > > CheckAdministra torRole = True
              > > Else
              > > CheckAdministra torRole = False
              > > End If
              > > End Function
              > >[/color]
              >
              > MSDN on 'IPrincipal.IsI nRole' and 'AppDomain.SetP rincipalPolicy' says:
              >
              > | *Requirements*
              > |
              > | *Platforms:* Windows 98, Windows NT 4.0, Windows
              > | Millennium Edition, Windows 2000, Windows XP Home Edition,
              > | Windows XP Professional, Windows Server 2003 family
              >
              > This means that the methods are supported by all versions of Windows running
              > the .NET Framework. The .NET Framework doesn't work together with Windows
              > 95. I suggest to check the documentation for the requirements of other
              > methods you are using too.
              >
              > Simpler solution for checking if the user is an administrator:
              >
              > \\\
              > Imports System.Security .Principal
              > .
              > .
              > .
              > Dim wp As New WindowsPrincipa l(WindowsIdenti ty.GetCurrent() )
              > Dim IsAdmin As Boolean = wp.IsInRole(Win dowsBuiltInRole .Administrator)
              > ///
              >
              > --
              > M S Herfried K. Wagner
              > M V P <URL:http://dotnet.mvps.org/>
              > V B <URL:http://classicvb.org/petition/>[/color]

              Comment

              Working...