Jump to content

C# Tutorial: Obtaining Windows User Details

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
rueleonheart

rueleonheart

    Newbie

  • Members
  • PipPip
  • 14 posts
The .NET Framework provides WindowsIdentity class that contains methods and properties for detecting the current windows user of the program. It contains the name of the current user, whether a user is authenticated, and the authentication type. There are also principal classes such as WindowsPrincipal and GenericPrincipal. Principals contain the identity and the roles a user belongs to. The following example shows you an example of using these classes.

using System;

using System.Security.Principal;

using System.Threading;


class Program

{

    public static void Main()

    {

        AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);


        WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;


        WindowsIdentity myIdentity = (WindowsIdentity)myPrincipal.Identity;


        Console.WriteLine("IdentityType: " + myIdentity.ToString());

        Console.WriteLine("Name: {0}", myIdentity.Name);

        Console.WriteLine("Member of Users? {0}", 

            myPrincipal.IsInRole(WindowsBuiltInRole.User));

        Console.WriteLine("Member of Administrators? {0}",

            myPrincipal.IsInRole(WindowsBuiltInRole.Administrator));

        Console.WriteLine("Authenticated: {0}", myIdentity.IsAuthenticated);

        Console.WriteLine("Anonymous: {0}", myIdentity.IsAnonymous);

    }

}

Note that the output will vary for you because you might be using a different user.

IdentityType: System.Security.Principal.WindowsIdentity

Name: COMPUTER\Username

Member of Users? True

Member of Administrators? True

Authenticated: True

Anonymous: False


We first imported two namespaces: System.Security.Principal and System.Threading. The first line of code inside Main hooks up the principal with the current windows account. The second line of code gets the Identity from the role and stores it in a WindowsIdentity object. The third line uses the ToString() method to simply show the type of the Identity. The following codes uses the properties of Principal and Identity to show the details desires. Notice that we used the WindowsBuiltInRole enumeration as an argument for the IsInRole method of the Principal class. WindowsBuiltInRole enumeration contains the roles that are installed in your system by default such as the administrator role.

#2
kresh7

kresh7

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 661 posts
Well i could get a hole bunch of information by using Windows enviroment variables.
Or by using some API's.
Eventhought i hate .NET nice snippet.
Posted Image

#3
opwuaioc

opwuaioc

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 216 posts
Thanks! I'm just now getting into C# and .NET programming, so any snippets like this are useful. :)
Something witty here.

#4
kresh7

kresh7

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 661 posts
Check the System.Environment Class :D check it's methods and procedures.
Posted Image

#5
Jarryd

Jarryd

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts

Quote

Check the System.Environment Class check it's methods and procedures.

The environment class provides information about, and means to manipulate, the current environment and platform. This class cannot be inherited.

Here's an example created by microsoft, Showing what you can actually do with this class,

using System;

using System.Collections;


class Sample 

{

    public static void Main() 

    {

    String str;

    String nl = Environment.NewLine;


    Console.WriteLine();

    Console.WriteLine("-- Environment members --");


    Console.WriteLine("CommandLine: {0}", Environment.CommandLine);


    String[] arguments = Environment.GetCommandLineArgs();

    Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments));


    Console.WriteLine("CurrentDirectory: {0}", Environment.CurrentDirectory);


    Console.WriteLine("ExitCode: {0}", Environment.ExitCode);


    Console.WriteLine("HasShutdownStarted: {0}", Environment.HasShutdownStarted);


    Console.WriteLine("MachineName: {0}", Environment.MachineName);


    Console.WriteLine("NewLine: {0}  first line{0}  second line{0}  third line", Environment.NewLine);


    Console.WriteLine("OSVersion: {0}", Environment.OSVersion.ToString());


    Console.WriteLine("StackTrace: '{0}'", Environment.StackTrace);


    Console.WriteLine("SystemDirectory: {0}", Environment.SystemDirectory);


    Console.WriteLine("TickCount: {0}", Environment.TickCount);


    Console.WriteLine("UserDomainName: {0}", Environment.UserDomainName);


    Console.WriteLine("UserInteractive: {0}", Environment.UserInteractive);


    Console.WriteLine("UserName: {0}", Environment.UserName);


    Console.WriteLine("Version: {0}", Environment.Version.ToString());


    Console.WriteLine("WorkingSet: {0}", Environment.WorkingSet);

    String query = "My system drive is %SystemDrive% and my system root is %SystemRoot%";

  

  str = Environment.ExpandEnvironmentVariables(query);

    Console.WriteLine("ExpandEnvironmentVariables: {0}  {1}", nl, str);


    Console.WriteLine("GetEnvironmentVariable: {0}  My temporary directory is {1}.", nl,

                           Environment.GetEnvironmentVariable("TEMP"));


    Console.WriteLine("GetEnvironmentVariables: ");

    IDictionary	environmentVariables = Environment.GetEnvironmentVariables();

    foreach (DictionaryEntry de in environmentVariables)

        {

        Console.WriteLine("  {0} = {1}", de.Key, de.Value);

        }


    Console.WriteLine("GetFolderPath: {0}", 

                 Environment.GetFolderPath(Environment.SpecialFolder.System));


    String[] drives = Environment.GetLogicalDrives();

    Console.WriteLine("GetLogicalDrives: {0}", String.Join(", ", drives));

    }

}


Posted Image