指定した Windows アカウント トークンによって表されるユーザーを表す、WindowsIdentity クラスの新しいインスタンスを初期化します。
名前空間: System.Security.Principal
アセンブリ: mscorlib (mscorlib.dll 内)
構文
'宣言
Public Sub New ( _
userToken As IntPtr _
)
'使用
Dim userToken As IntPtr
Dim instance As New WindowsIdentity(userToken)
public WindowsIdentity (
IntPtr userToken
)
public:
WindowsIdentity (
IntPtr userToken
)
public WindowsIdentity (
IntPtr userToken
)
public function WindowsIdentity (
userToken : IntPtr
)
適用できません。
パラメータ
- userToken
コードが実行されている対象ユーザーのアカウント トークン。
例外
例外の種類 | 条件 |
---|---|
userToken が 0 です。 または userToken は、複製され、偽装に対して無効になっています。 |
|
呼び出し元に、正しいアクセス許可がありません。 または Win32 エラーが発生しました。 |
解説
WindowsIdentity のインスタンスの初期プロパティ値を次の表に示します。
プロパティ |
初期値 |
---|---|
NTLM |
|
Normal |
|
false |
![]() |
---|
userToken で表されるトークンは、Win32 API の LogonUser 関数などのアンマネージ コードを呼び出すことで取得できます。userToken は、常に Win32 API の Close 関数を呼び出して解放してください。アンマネージ コードの呼び出しの詳細については、「アンマネージ DLL 関数の処理」を参照してください。 |
使用例
WindowsIdentity コンストラクタを使用し、現在の Windows アカウント トークンで表されるユーザーを表す WindowsIdentity クラスの新しいインスタンスを作成するコード例を次に示します。WindowsIdentity オブジェクトは、トークン所有者のセキュリティ識別子、および現在のユーザーが属しているグループの ID 参照を取得する場合に使用します。
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
using System;
using System.Security.Principal;
using System.Runtime.InteropServices;
class WindowsIdentityMembers
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);
[STAThread]
static void Main(string[] args)
{
IntPtr accountToken = WindowsIdentity.GetCurrent().Token;
WindowsIdentity windowsIdentity = new WindowsIdentity(accountToken);
SecurityIdentifier si = windowsIdentity.Owner;
Console.WriteLine(si.ToString());
si = windowsIdentity.User;
Console.WriteLine(si.ToString());
IdentityReferenceCollection irc = windowsIdentity.Groups;
foreach (IdentityReference ir in irc)
{
Console.WriteLine(ir.Value);
}
TokenImpersonationLevel token = windowsIdentity.ImpersonationLevel;
Console.WriteLine(token.ToString());
// Free the account token.
if (accountToken != IntPtr.Zero)
CloseHandle(accountToken);
}
}
.NET Framework のセキュリティ
- SecurityPermission (プリンシパル オブジェクトを操作するために必要なアクセス許可)。SecurityPermissionFlag.ControlPrincipal (関連する列挙体)。
- SecurityPermission (アンマネージ コードにアクセスするために必要なアクセス許可)。SecurityPermissionFlag.UnmanagedCode (関連する列挙体)
プラットフォーム
Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition
Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。
バージョン情報
.NET Framework
サポート対象 : 3.0,2.0,1.1,1.0
参照
関連項目
WindowsIdentity クラス
WindowsIdentity メンバ
System.Security.Principal 名前空間