Detecting remote users with Environment variables

mellomel70

Member
Joined
May 10, 2010
Messages
8
Programming Experience
10+
This is related to my post at: http://www.vbdotnetforums.com/asp-n...mi-net-detecting-remote-users-via-citrix.html. I have an ASP.Net website and I'm trying to tell whether someone is accessing it via their desktop or remoting into our company intranet via Citrix. If the user is using Citrix, they will connect to our Citrix server and then open a Terminal Services session to their own desktop. From there they'll open IE and go to my website. As described in the above-mentioned link, I've tried using WMI.Net to do this, but am not having any luck. Another approach I've tried is using Environment.GetEnvironmentVariable("SESSIONNAME"). From what I've seen on the Web, this will return "console" if the user is local or "RDP***" if the user is remote. Here's my code:

VB.NET:
Dim envPerm1 As New EnvironmentPermission(EnvironmentPermissionAccess.AllAccess, "TMP")
envPerm1.AddPathList(EnvironmentPermissionAccess.Read, "SESSIONNAME")
Dim sess As String = Environment.GetEnvironmentVariable("SESSIONNAME").ToLower()
        If sess <> "console" 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(Session("UserEID") & " " & DateTime.Now & " logged in remotely")
                oWrite.Close()
            Else
                Dim oWrite As New FileStream(LogFile, IO.FileMode.Append)
                Dim bytes As Byte() = New UTF8Encoding().GetBytes(Session("UserEID") & " " & DateTime.Now & " logged in remotely" & Environment.NewLine)
               oWrite.Write(bytes, 0, bytes.Length)
                oWrite.Close()
            End If
        End If

All runs well within VisualStudio, but the minute I go to my website using IE, the program chokes at the assignment of the variable sess and I get the error message: "Object reference not set to an instance of an object." I've also tried Environ("SESSIONNAME") with the same result.

I've also tried the following (which I shamelessly stole off the web):

VB.NET:
Private Declare Function GetEnvironmentVariable Lib "kernel32" _
            Alias "GetEnvironmentVariableA" (ByVal lpName As String, _
            ByVal lpBuffer As String, ByVal nSize As Long) As Long

Dim ret As Long, lngBuff As Long, strSession As String
lngBuff = 255
strSession = lngBuff.ToString()
ret = GetEnvironmentVariable("SESSIONNAME", strSession, lngBuff)
If strSession <> "console" 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(Session("UserEID") & " " & DateTime.Now & " logged in remotely")
      oWrite.Close()
   Else
      Dim oWrite As New FileStream(LogFile, IO.FileMode.Append)
      Dim bytes As Byte() = New UTF8Encoding().GetBytes(Session("UserEID") & " " & DateTime.Now & " logged in remotely" & Environment.NewLine)
      oWrite.Write(bytes, 0, bytes.Length)
      oWrite.Close()
   End If
End If

This doesn't throw an error message, but both local and remote users show as remote with this method. I'm stumped. Is there a way to do this in ASP.Net? :confused:

Thanks all!
 
Back
Top