WMI.Net detecting remote users via Citrix

mellomel70

Member
Joined
May 10, 2010
Messages
8
Programming Experience
10+
Hi - I've got WMI.Net code in an ASP.Net page that detects remote logins via Terminal Services. It works fine. My code is below:


VB.NET:
Public Sub Page_Load(ByVal s As Object, ByVal e As EventArgs) Handles Me.Load
    If impersonateValidUser("MyUserID", "MyDomain", "MyPassword") Then
      'Insert your code that runs under the security context of a specific user here.
      
'Initialize an object searcher with this query
      Dim query As ObjectQuery
      query = New ObjectQuery("Select * from Win32_LogonSession Where LogonType = 10")
      Dim searcher As ManagementObjectSearcher
      searcher = New ManagementObjectSearcher(query)
      'Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_LogonSession")
      Dim queryCollection1 As ManagementObjectCollection = searcher.Get()

      'Get the resulting collection and loop through it
      Dim mo As ManagementBaseObject
      For Each mo In queryCollection1
        query = New ObjectQuery("Associators of " & "{Win32_LogonSession.LogonId=" & mo("LogonId") & "} " & "Where AssocClass=Win32_LoggedOnUser Role=Dependent")
      
        Dim searcher2 = New ManagementObjectSearcher(query)
        For Each envVar As ManagementObject In searcher2.Get()
          If envVar("Name") = Session("UserEID") Then
            Dim path As String = Request.PhysicalApplicationPath()
            Dim LogFile As String = path & "atdfile.txt"
            If File.Exists(LogFile) = False Then
              Dim oWrite As StreamWriter
              oWrite = File.CreateText(LogFile)
              oWrite.WriteLine(envVar("Name") & " " & DateTime.Now & " logged in remotely")
              oWrite.Close()
            Else
              Dim oWrite As New FileStream(LogFile, IO.FileMode.Append)
              Dim bytes As Byte() = New UTF8Encoding().GetBytes(envVar("Name") & " " & DateTime.Now & " logged in remotely" & Environment.NewLine)
              oWrite.Write(bytes, 0, bytes.Length)
              oWrite.Close()
            End If
          End If
        Next
      Next
      undoImpersonation()
      'Else 'Your impersonation failed. Therefore, include a fail-safe mechanism here.
    End If
    Response.Redirect("http://MyWebsite")
  End Sub

However, I need to detect users who are using Citrix to connect to their computer via Terminal Services. So, in other words, the user connects to our Citrix farm. The user then opens a terminal services session and connects to his or her computer and then opens my web page. When this happens, WMI does NOT see this user as connected remotely. I've tried modifying my code slightly by adding in this code:

VB.NET:
Dim options As New ConnectionOptions
      options.Username = "MyUserID"
      options.Password = "MyPassword"

      Dim scope As New ManagementScope("\\.\root\Citrix", options)

Plus I've added the scope to the query like so:

VB.NET:
searcher = New ManagementObjectSearcher(scope, query)

With this modified code, I get a very unhelpful error message:
An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.

Does anyone know how to do this?

Thanks!
 
Back
Top