vasistbhargav / cassia

Automatically exported from code.google.com/p/cassia
0 stars 0 forks source link

Access Denied on GetSessions() VB.Net #46

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1.GetSessions()

What is the expected output? What do you see instead?
The usernames that are connected to the terminal server( the sessions). Instead 
i get access denied. I have enabled RPC connections on both server and client 
PC. still get access denied issue. 

What version of the product are you using? On what operating system?
The Latest version. Running software on Windows 7, Terminal Server is 2008 R2 
Enterprise

Please provide any additional information below.
Here is my code. 
Imports Cassia
Imports System
Imports System.Security.Principal
Imports System.Web
Imports System.Net.Security
Imports System.Runtime
Imports System.Runtime.InteropServices
Imports System.ComponentModel

Public Class Main

    Dim LOGON32_LOGON_INTERACTIVE As Integer = 2
    Dim LOGON32_PROVIDER_DEFAULT As Integer = 0

    Dim impersonationContext As WindowsImpersonationContext

    Declare Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As String, _
                            ByVal lpszDomain As String, _
                            ByVal lpszPassword As String, _
                            ByVal dwLogonType As Integer, _
                            ByVal dwLogonProvider As Integer, _
                            ByRef phToken As IntPtr) As Integer

    Declare Auto Function DuplicateToken Lib "advapi32.dll" ( _
                            ByVal ExistingTokenHandle As IntPtr, _
                            ByVal ImpersonationLevel As Integer, _
                            ByRef DuplicateTokenHandle As IntPtr) As Integer

    Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
    Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long

    Public Function RunAsAdmin() As String
        impersonateValidUser(tbusername.Text, tbdomain.Text, tbpassword.Text)
    End Function

    Public Function RunAsUser()
        undoImpersonation()
    End Function

    Private Function impersonateValidUser(ByVal userName As String, _
    ByVal domain As String, ByVal password As String) As Boolean
        Dim tempWindowsIdentity As WindowsIdentity
        Dim token As IntPtr = IntPtr.Zero
        Dim tokenDuplicate As IntPtr = IntPtr.Zero
        impersonateValidUser = False

        If RevertToSelf() Then
            If LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
                If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
                    tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)
                    impersonationContext = tempWindowsIdentity.Impersonate()
                    If Not impersonationContext Is Nothing Then
                        impersonateValidUser = True
                    End If
                End If
            End If
        End If
        If Not tokenDuplicate.Equals(IntPtr.Zero) Then
            CloseHandle(tokenDuplicate)
        End If
        If Not token.Equals(IntPtr.Zero) Then
            CloseHandle(token)
        End If
    End Function

    Private Sub undoImpersonation()
        impersonationContext.Undo()
    End Sub
    Private Sub Main_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        tbdomain.Text = My.Settings.Domain
        tbserver.Text = My.Settings.Server
        tbusername.Text = My.Settings.Username
        tbpassword.Text = My.Settings.Password
        tbFirstRun.Text = My.Settings.FirstRun
        If tbFirstRun.Text = "1" Then
            FirstRunSetup.Show()
        End If
    End Sub
    Private Sub Main_FormClosing(sender As System.Object, e As System.EventArgs) Handles MyBase.FormClosing
        My.Settings.Domain = tbdomain.Text
        My.Settings.Server = tbserver.Text
        My.Settings.Username = tbusername.Text
        My.Settings.Password = tbpassword.Text
        My.Settings.FirstRun = tbFirstRun.Text
        undoImpersonation()
    End Sub
    Private Sub btnreset_Click(sender As System.Object, e As System.EventArgs) Handles btnreset.Click
        Dim ser As String = tbserver.Text
        RunAsAdmin()
        Dim manager As ITerminalServicesManager = New TerminalServicesManager()
        Using server As ITerminalServer = manager.GetRemoteServer(ser)
            server.Open()

            For Each session As ITerminalServicesSession In server.GetSessions()
                Dim SelectedItems As ListView.SelectedListViewItemCollection = _
CType(sender, ListView).SelectedItems
                If (SelectedItems.Count > 0) Then
                    If session.UserName = SelectedItems(0).SubItems(0).Text Then
                        manager.CurrentSession.Disconnect()
                    End If
                End If
            Next
        End Using
    End Sub

    Private Sub btnconnect_Click(sender As System.Object, e As System.EventArgs) Handles btnconnect.Click
        Dim mgr As Cassia.ITerminalServicesManager = New Cassia.TerminalServicesManager()
        Dim serverAddr As String = tbserver.Text
        Dim serverDomain As String = tbdomain.Text
        Dim serverUsername As String = tbusername.Text
        Dim serverPassword As String = tbpassword.Text
        Using serverInstance = mgr.GetRemoteServer(serverAddr)
            serverInstance.Open()
            MsgBox(serverInstance.ServerName.ToString)
            For Each sessions As ITerminalServicesSession In serverInstance.GetSessions()
                If (sessions.ConnectionState = ConnectionState.Disconnected) OrElse (sessions.ConnectionState = ConnectionState.Active) AndAlso (sessions.IdleTime > TimeSpan.FromMinutes(1)) Then
                    Dim SubItems(4) As String
                    SubItems(0) = sessions.UserName
                    SubItems(1) = sessions.ClientName
                    SubItems(2) = sessions.CurrentTime
                    SubItems(3) = sessions.ConnectionState
                    lvusers.Items.Add(New ListViewItem(SubItems))

                    serverInstance.Close()
                End If
            Next
        End Using
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        FirstRunSetup.Show()
    End Sub
End Class

Any help would be much appreciated. 
Cheers
Chris

Original issue reported on code.google.com by c.nan...@gmail.com on 5 Nov 2011 at 2:47

GoogleCodeExporter commented 8 years ago
Chris,

Could you post a stack trace for the access denied exception? Also, closing the 
connection to the server with "serverInstance.Close()" in the middle of the For 
Each loop in btnconnect_Click looks rather suspicious to me. I think that line 
should be after the "Next" statement.

Original comment by danports on 8 Nov 2011 at 5:01

GoogleCodeExporter commented 8 years ago
Closing since there's not enough information to reproduce this. Chris, if you 
have anything to add, just post a comment.

Original comment by danports on 11 Dec 2011 at 1:23

GoogleCodeExporter commented 8 years ago
The attached document is a screenshot of the Stack Trace. Im not sure if ive 
done the stack trace properly. However I placed it after the "Next" before the 
"else".

Cheers
Chris

Original comment by c.nan...@gmail.com on 24 Dec 2011 at 1:23

Attachments:

GoogleCodeExporter commented 8 years ago
I meant the stack trace associated with the exception -- there's no Cassia code 
in the stack trace above. Usually when there's an exception, Visual Studio will 
pop up a dialog out of which you can copy the stack trace text if you poke 
around and look for it.

Original comment by danports on 24 Dec 2011 at 1:40

GoogleCodeExporter commented 8 years ago
I have practically the same problem.  Attached is shot of the stack trace when 
the exception is generated - after the call to WTSEnumerateSessions().  Using 
32 vs. 64 bit makes no difference, there are no firewalls, RDP connects easily 
over port 3389, telnet has no problems with port 445, etc, etc.  I am all out 
of ideas!
When I get some more time I'll revisit and try added GetLastError() calls, but 
I'm pretty sure it'll just say 'Access denied' anyway.

Original comment by grimreap...@gmail.com on 8 Mar 2012 at 11:54

Attachments:

GoogleCodeExporter commented 8 years ago
@grimreaper32uk: Are you connecting to the local server or a remote server? If 
local, could UAC be an issue in your scenario? If remote, are you connecting to 
a Windows 7/Vista/XP machine? Have you checked that the user account you're 
running under has the appropriate Remote Desktop permissions?

Original comment by danports on 8 Mar 2012 at 2:16