Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 301256 - Last Review: July 15, 2004 - Revision: 3.3
How To Check the Windows Identity in a Client Application in Windows .NET Framework
This article was previously published under Q301256
This article demonstrates how to use the classes in the
System.Security.Principal namespace to check the user's Microsoft Windows user name and
group memberships from a client application.
Requirements
The following list outlines the recommended hardware, software,
network infrastructure, and service packs that you will need:
- Windows 2000 Professional, Windows 2000 Server, Windows
2000 Advanced Server, or Windows NT 4.0 Server
- Microsoft Visual Studio .NET
This article assumes that you are familiar with the following
topics:
- Visual Studio .NET
- Windows security
How to Check the Windows Identity in a Client Application
- Open Visual Studio .NET.
- Create a new Console Application in Visual Basic
.NET.
- Use
the Imports statement on the System.Security.Principal namespace so that you are not required to qualify WindowsPrincipal and WindowsIdentity declarations later in your code. You must use the Imports statement prior to any other declarations.
Imports System.Security.Principal
- Make a call to the SetPrincipalPolicy method of the CurrentDomain object, and set the WindowsPrincipal class so that it is attached to the thread. Without this call,
the principal that is returned is a GenericPrincipal class that contains no user information.
Add this code to the Main method
of Module1.
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
NOTE: The call to SetPrincipalPolicy requires the ControlPrincipal property SecurityPermission class, which is not normally given out to less than fully-trusted
code. This prevents semi-trusted code (such as code that runs off the Internet)
from gaining access to a user's account name. - Declare a WindowsPrincipal object, and use System.Threading.Thread.CurrentPrincipal to access the current principal from the Thread class. Because this method returns an IPrincipal interface, it must be cast as a WindowsPrincipal object before you can use it as one.
Dim user As WindowsPrincipal = CType(System.Threading.Thread.CurrentPrincipal, WindowsPrincipal)
- Declare a WindowsIdentity object to hold the identity information of the user. Use the Identity property of the WindowsPrincipal object.
Dim ident As WindowsIdentity = user.Identity
- Alternately, if you only want to get the user's identity,
use the GetCurrent static method as a shortcut to steps 5 and 6. The principal's
information can then be retrieved from the user's identity.
Dim ident As WindowsIdentity = WindowsIdentity.GetCurrent()
Dim user As New WindowsPrincipal(ident)
- Use the Name property to retrieve the user's name, and use the AuthenticationType property to display that to the console.
Console.WriteLine("User name: {0}", ident.Name)
Console.WriteLine("Authentication type: {0}", ident.AuthenticationType)
- Use the IsInRole method of the WindowsPrincipal object to see whether the user is in various, built-in groups (or
others).
Console.WriteLine("Is in Administrators group: {0}", user.IsInRole(WindowsBuiltInRole.Administrator))
Console.WriteLine("Is in Guests group: {0}", user.IsInRole(WindowsBuiltInRole.Guest))
If you are checking role group membership to deny access to an
application (and not to customize the user experience), an even simpler
approach is to use the PrincipalPermission class to demand the required role. - Add Console.ReadLine to the end of the procedure to easily view the results.
Console.ReadLine() 'Pause
- Run the project to test the results.
- Save and close the project.
For more information about the
Principal and the
Identity objects, see the following Microsoft .NET Framework Developer's
Guide documentation:
For more information about key security concepts, see the
following Microsoft .NET Framework Developer's Guide documentation:
For another exposition of these concepts and a similar sample,
see the .NET QuickStarts, which are installed with the .NET Framework Software
Development Kit (SDK) and available at the following location on your computer:
http://localhost/QuickStart/HowTo/doc/security/WindowsIdentityCheck.aspx
For more information about the Windows
Identity and
Principal classes, see the .NET Framework Class Library documentation.
APPLIES TO
- Microsoft .NET Framework 1.1
- Microsoft .NET Framework 1.0
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate