sanfilip / cassia

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

Access Denied error only when trying to log off a user #81

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Code to log off disconnected user
2. Try to log them off
3. Receive "Access Denied" error even when local using elevated privileges.

What is the expected result? What happens instead?
The user should be logged off, Receive "Access Denied error, even when 
privileges are elevated.

What version of Cassia are you using (e.g. 2.0.0.60)?
2.0.0.60

On what Windows version is your code running?
Windows 8.1 and/or Windows 2012 R2 both 64bit

Is your code running as a Windows service, an ASP.NET application, or a
standalone application?

Stand alone

What Windows version is running on the remote server you are trying to
access (if any)?

Windows 2012 R2

Please provide any additional information below. If you are running into an 
exception, please include a stack trace (at least the portion of it that 
relates to Cassia).

Looked through documentation for an area to put an administrative UID and 
password but found nothing.  I also searched through existing tickets and found 
some information but all that seems to be "All Connections"  I only get it when 
trying to log someone out.

Thank you for your time.

Original issue reported on code.google.com by jather...@northpennlegal.org on 12 Jun 2015 at 12:50

GoogleCodeExporter commented 8 years ago
Access denied errors generally indicate either a firewall issue or a 
permissions issue. Issue 34 has some information on how to impersonate other 
users (e.g. administrators). Is your code running as an account that has 
administrator privileges on the local/remote machine? Did you make any changes 
to the standard Remote Desktop permissions? (See 
https://technet.microsoft.com/en-us/library/cc753032.aspx)

Original comment by danports on 12 Jun 2015 at 1:04

GoogleCodeExporter commented 8 years ago
This is the code I used based on the code used in Issue 34, No matter what 
combination I try it still shows access denied, Firewall is off at the moment.  
As I stated, I am an administrator from the remote development pc as well as 
the actual Terminal Server.  Any help would be appreciated...

 Private Sub btnConnectRemote_Click(sender As Object, e As EventArgs) Handles btnConnectRemote.Click
        Dim serverName As String = InputBox("Connect to Server")
        Dim usrName As String = String.Empty
        Dim token As IntPtr

        If Not NativeMethods.LogonUser("Administrator", serverName, "adminpassword", NativeMethods.LogonType.NewCredentials, NativeMethods.LogonProvider.Default, token) Then
            Throw New Win32Exception
        End If

        Try
            Dim tokenDuplicate As IntPtr

            If Not NativeMethods.DuplicateToken(token, NativeMethods.SecurityImpersonationLevel.Impersonation, tokenDuplicate) Then
                Throw New Win32Exception()
            End If

            Try
                Using impersonationContext As WindowsImpersonationContext = New WindowsIdentity(tokenDuplicate).Impersonate
                    Using serverInstance = iTSM.GetRemoteServer(serverName)
                        serverInstance.Open()

                        rtbList.Text = "Retreiving User List..."
                        For Each session As ITerminalServicesSession In serverInstance.GetSessions
                            If session.ConnectionState = ConnectionState.Disconnected Then
                                usrName = session.UserName
                                session.Logoff(True)
                                rtbList.Text &= String.Concat(usrName, " has been logged off...")
                            End If
                        Next

                        For Each session As ITerminalServicesSession In server.GetSessions
                            rtbList.Text &= String.Concat(session.UserAccount, vbTab, vbTab, " Session ID: ", session.SessionId, vbTab, vbTab, " Connection State:", session.ConnectionState, vbCrLf)
                        Next

                        serverInstance.Close()
                    End Using
                    impersonationContext.Undo()
                End Using
            Catch ex As Exception
                rtbList.Text &= String.Concat("Error Occured:", ex.ToString)
            Finally
                If tokenDuplicate <> IntPtr.Zero Then
                    NativeMethods.CloseHandle(tokenDuplicate)
                End If
            End Try

        Catch ex As Exception

        Finally
            If token <> IntPtr.Zero Then
                NativeMethods.CloseHandle(token)
            End If
        End Try

    End Sub

Original comment by jather...@northpennlegal.org on 12 Jun 2015 at 3:29

GoogleCodeExporter commented 8 years ago
Close this ticket, I found that it was attempting to disconnect a system user 
with no username attached, I have added code to address this issue, and it 
seems to be working now.  Thank you for your information.

Original comment by jather...@northpennlegal.org on 12 Jun 2015 at 5:05

GoogleCodeExporter commented 8 years ago
Thanks for the update.

Original comment by danports on 12 Jun 2015 at 5:15