WindowsIdentity Constructor (IntPtr)
This page is specific to:.NET Framework Version:2.03.03.54.0
.NET Framework Class Library
WindowsIdentity Constructor (IntPtr)

Initializes a new instance of the WindowsIdentity class for the user represented by the specified Windows account token.

Namespace:  System.Security.Principal
Assembly:  mscorlib (in mscorlib.dll)
Syntax

'Usage

Dim userToken As IntPtr

Dim instance As New WindowsIdentity(userToken)

'Declaration

Public Sub New ( _
    userToken As IntPtr _
)

Parameters

userToken
Type: System..::.IntPtr
The account token for the user on whose behalf the code is running.
Exceptions

ExceptionCondition
ArgumentException

userToken is 0.

-or-

userToken is duplicated and invalid for impersonation.

SecurityException

The caller does not have the correct permissions.

-or-

A Win32 error occurred.

Remarks

The following table shows initial property values for an instance of WindowsIdentity.

Property

Initial Value

AuthenticationType

Negotiate

WindowsAccountType

Normal

IsAuthenticated

false

NoteNote:

You can retrieve the token represented by userToken by calling unmanaged code such as the Win32 API LogonUser function. Always release userToken by calling the Win32 API Close function. For more information on calling unmanaged code, see Consuming Unmanaged DLL Functions.

Examples

The following code example shows the use of the WindowsIdentity constructor to create a new instance of the WindowsIdentity class for the user represented by the current Windows account token. The WindowsIdentity object is used to get the security identifier for the token owner and the identity references for the groups the current user belongs to.

Imports System
Imports System.Security.Principal

Class WindowsIdentityMembers

    Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Boolean

    <STAThread()> _
    Shared Sub Main(ByVal args() As String)
        Dim accountToken As IntPtr
        accountToken = WindowsIdentity.GetCurrent().Token
        Dim windowsIdentity1 As New WindowsIdentity(accountToken)
        Dim si As SecurityIdentifier = windowsIdentity1.Owner
        Console.WriteLine(si.ToString())
        si = windowsIdentity1.User
        Console.WriteLine(si.ToString())
        Dim irc As IdentityReferenceCollection = windowsIdentity1.Groups
        Dim ir As IdentityReference
        For Each ir In irc
            Console.WriteLine(ir.Value)
        Next ir
        Dim token As TokenImpersonationLevel = windowsIdentity1.ImpersonationLevel
        Console.WriteLine(token.ToString())
        ' Free the tokens.
        If Not System.IntPtr.op_Equality(accountToken, IntPtr.Zero) Then
            CloseHandle(accountToken)
        End If

    End Sub 'Main 
End Class 'WindowsIdentityMembers


.NET Framework Security

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Community Content

use Dispose instead of CloseHandle
Added by:Mansatan
The example explicitly releases the account token using call to unmanaged code. although "WindowsIdentity.Token" page states: "Do not explicitly release the account token. The token is released by the Dispose method that you can call in code or that is automatically called by the garbage collector."
But DO call CloseHandle if you called LogonUser
Added by:Alan McBee
The constructor for WindowsIdentity internally calls DuplicateHandle to create the handle that is closed in the Dispose method. If you passed in your own token handle using any variant of the constructor with an IntPtr argument, then you will need to call CloseHandle after creating the WindowsIdentity instance.
© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View