在VB.NET中获取当前的Windows用户会话ID

时间:2016-03-10 16:24:41

标签: vb.net session winlogon

我需要知道会话是否属于当前用户。我以为我会做这样的事情:

Public Function SessionID()
    Dim session_id = 0
    Dim user As System.Security.Principal.WindowsIdentity
    user = System.Security.Principal.WindowsIdentity.GetCurrent()
    Dim selectQuery As SelectQuery = New SelectQuery("Win32_LogonSession")
    Dim searcher As New ManagementObjectSearcher(selectQuery)
    For Each session As ManagementObject In searcher.Get
        If session("LogonID") = user.Name Then
            session_id = Convert.ToInt32(session("LogonID"))
        End If
    Next
    Return session_id
End Function

SelectQuery返回当前会话列表但我找不到当前用户的登录ID以进行比较

0 个答案:

没有答案