Tech Off Thread

8 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

bool IsAdmin()

Back to Forum: Tech Off
  • User profile image
    candseeme

     /// <summary>
      /// this method used to check if The User is an admin or not
      /// </summary>
      /// <returns>true if it is an admin othewise false</returns>

      bool IsAdmin()
      {
       // check if it is a windows nt os
       // because win 9x does not have user groups administrators, users, power users and etc..
       if ( Environment.OSVersion.ToString().StartsWith ( "Microsoft Windows NT" ) )
       {
        WindowsPrincipal wp = new WindowsPrincipal ( WindowsIdentity.GetCurrent() );

        if ( wp.IsInRole ( WindowsBuiltInRole.Administrator ) )
        {
         return true;
        }
        else
        {
         return false;
        }
       }
       else
       {
        // return true cause we do not need the check
        // this way this program will run on any windows
        return true;
       }
       
      }

    any better thoughts !!

    thanks

    Cool

  • User profile image
    W3bbo

    Remember that .NET apps aren't limited to the Windows OS: .NET is cross platform and can be executed under UNIX-Derivatives (inc. Linux) and MacOS-X and they all have their equivalents of 'Administrative' users.

  • User profile image
    candseeme

    W3bbo wrote:
    Remember that .NET apps aren't limited to the Windows OS: .NET is cross platform and can be executed under UNIX-Derivatives (inc. Linux) and MacOS-X and they all have their equivalents of 'Administrative' users.


    this is what Microsoft wants you to belive

    But belive it or not it will never actually support any operating system other than its own

    till now its framework avaliable only for windows and this will last forever belive me W3BBO

  • User profile image
    W3bbo

    At risk of this becoming a flamewar...

    candseeme wrote:
    this is what Microsoft wants you to belive


    Wrong.

    Microsoft doesn't put an ounce of support into cross-platform .NET with the exception of releasing ROTOR and a few other tidbits.

    Most of the development for MONO (cross-platform .NET) is coming from Novell.

    candseeme wrote:
    But belive it or not it will never actually support any operating system other than its own


    ...then why did they release WPF for OS-X and X-Windows?

    candseeme wrote:
    till now its framework avaliable only for windows and this will last forever belive me W3BBO


    It's been around for Linux, OS-X, FreeBSD, and others for years now. Please get your facts right before posting.

  • User profile image
    candseeme

    thank you w3bbo

  • User profile image
    AndyC

    Think long and hard about why you are doing this before you do. If the code you're writing isn't for administering a Windows system then don't check the user is an Administrator.

    It is much, much better to check the user has permission to perform an operation than to make assumptions based on group membership (which may not hold true in a customised environment)

  • User profile image
    candseeme

    AndyC wrote:
    Think long and hard about why you are doing this before you do. If the code you're writing isn't for administering a Windows system then don't check the user is an Administrator.

    It is much, much better to check the user has permission to perform an operation than to make assumptions based on group membership (which may not hold true in a customised environment)


    Thanks AndyC

    the reason why i'm doing this check is because i'm writing a windows program to fix and repair the windows registry
    so you must first check to see if the user is admin or not

    so this complete it

    //to check if the user is admin or not

    if ( !IsAdmin() )

    {

    string TheMsg = "\n An Access Denied error was returned while attempting to read from the registry. " +

    "\n You may need to log on using an Administrator account to make the specified changes.";

    MessageBox.Show ( " Sorry Mr \"" + Environment.UserName + TheMsg , "Admin Not Detected",

    MessageBoxButtons.OK, MessageBoxIcon.Error );

    Application.ExitThread();

    Application.Exit();

    this.Close();

    }

    else

    {

    CopyContextMenu();

    CoreMethod( true, true );

    MyAction.ApplyViewOptions ( View_Main );

    }

  • User profile image
    AndyC

    candseeme wrote:


    Thanks AndyC

    the reason why i'm doing this check is because i'm writing a windows program to fix and repair the windows registry
    so you must first check to see if the user is admin or not



    Administrators do not necessarily have permission to read / write all registry keys (in fact there are some they cannot read with the default permissions), also the default permissions allow Power Users to modify the same keys that Administrators can.

    Don't make assumptions, check permissions on keys individually. A little extra effort now will stop your application breaking completely on a locked-down system. User Account Protection on Vista will make this even more important.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.