VB.net Terminal Server application - vb.net

im working on an terminal service application for personal use that allows me to list and manage all my RDP sessions via tabs (I know these exists, but using it as a small learning project also). I was wondering if anyone could help with a few items. I am trying to dynamically create and add tabs and the RDP control onto each tab. This seems to be working OK for creating the first tab page and control but fails on the subsequent ones with the error below Code below:
Dim theNewTabPage As New DevExpress.XtraTab.XtraTabPage()
theNewTabPage.Text = txtserver.Text
theNewTabPage.Name = Minute(DateTime.Now) & Second(DateTime.Now)
Dim RDPCONTROL As New AxMSTSCLib.AxMsRdpClient9NotSafeForScripting
RDPCONTROL.Dock = DockStyle.Fill
'Just a test to ensure unique name
RDPCONTROL.Name = Minute(DateTime.Now) & Second(DateTime.Now)
XtraTabControl1.TabPages.Add(theNewTabPage)
theNewTabPage.Controls.Add(RDPCONTROL)
Try
RDPCONTROL.Server = txtserver.Text
RDPCONTROL.Domain = txtdomain.text
RDPCONTROL.UserName = txtusername.Text
Dim secured As MSTSCLib.IMsTscNonScriptable = DirectCast(RDPCONTROL.GetOcx(), MSTSCLib.IMsTscNonScriptable)
secured.ClearTextPassword = txtpassword.Text
RDPCONTROL.Connect()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try`
Error when creating the second connection:
Using ex.message: exception of type 'system.windows.forms.axhost+invalidactivexstateexception was thrown
Using ex.tostring: property set of 'Server' cannot be invoked at this time
The next item is how can i detect if the RDP session was disconnected on the tab and then pop-up a message and close the required tab

After a bit of playing around i was able to solve this by calling:
XtraTabControl1.SelectedTabPage = newTabPage
Just after i added the new tab page.
Hope this helps someone in the future

Related

Error thrown trying launch a webpage from a button

I created a button and a menu item in vb.net (.NET 6). I found several answers here on SO that say the process to launching a webpage from such an event can be launched with this code:
Dim webAddress As String = "http://www.example.com/"
Process.Start(webAddress)
However, trying launch the code, I'm given the error of "system cannot find the file specified".
Looking more into it, I know that .NET 6 is running a bit differently and changed the code to the following:
Using link As New Process()
link.StartInfo.UseShellExecute = True
link.Start(New ProcessStartInfo("https://example.com"))
End Using
But still to no avail, and I am given the same error. "System cannot find the file specified." I can run addresses via the regular Windows Run prompt... but the program still cannot launch.
Following Jimi's comment to my original question, I changed the Sub to the following:
Sub LaunchWebsite(strWebpageURL As String)
Using Process.Start(New ProcessStartInfo(strWebpageURL) With {.UseShellExecute = True})
End Using
End Sub
Using this, the webpage launched in my desktop's default browser with no problem.
You can use
Respone.Redirect("http://www.example.com/")
or use javascript in server side code
Dim url As String = "http://www.example.com"
Dim s As String = "window.open('" & url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');"
ClientScript.RegisterStartupScript(Me.GetType(), "script", s, True)
Above code open webpage in new popup window.
Regards
Aravind

Run Internet Explorer as a different user

I'm trying to launch Internet Explorer with a different user to access a website that requires single sign on.
Below is the code that I'm using. It doesn't produce any errors but IE doesn't launch at all, not even the process.
Try
System.Diagnostics.Process.Start(IExplorerPath, Username, ConvertToSecureString(Password), Domain)
Success = True
Catch ex As Exception
Success = False
Error_Message = ex.Message
End Try
I also tried the following variation with the same result (nothing):
Try
Dim psi As New ProcessStartInfo()
psi.Filename = IExplorerPath
psi.UserName = Username
psi.Domain = Domain
psi.Password = ConvertToSecureString(Password)
psi.UseShellExecute = False
Process.Start(psi)
Success = True
Catch ex As Exception
Success = False
Error_Message = ex.Message
End Try
This is the ConvertToSecureString function:
Function ConvertToSecureString(ByVal str As String)
Dim password As New SecureString
For Each c As Char In str.ToCharArray
password.AppendChar(c)
Next
Return password
End Function
Ok, I've been searching a lot a solution for this and if you have it you deserve a Nobel price. In the mean time, if it helps anyone this is the workaround that I had to use:
Use Process.Start to run a .bat file that contains the following code:
runas /user:User#Domain.com "C:\Program Files\Internet Explorer\iexplore.exe"
Then, when the command prompt appears I use the Send Keys to type in the password and press Enter.
This launches IE with different credentials. But again, there should be a better way.
This may / may not work. Try to launch your website that requires single sign on in "CHROME", hit F-12, go to Application Tab -> Cookies -> Click on your site link. on left hand side look for something that represent your session id, may be JSESSIONID or similar, copy that. Now open your Internet Explorer, hit F-12 and manually create that JSESSIONID by running this command in console window
document.cookie = "JSESSIONID=<your-session-id-from-chrome"
hit play button to execute script
Refresh your browser

Data Is not being added to the database

I finally got another issue I was having worked out. Now I am having an issue updating a database with my code.
The data gets put in just fine, and If I loop through the data, it shows that it is there. But when I open the DataSet Designer and "Preview Data" it doesn't show the new data. All this runs smoothly and I get no Errors or exceptions.
CUsersTableAdapter.Fill(MasterDataSet.CUsers)
Dim AddRow As MasterDataSet.CUsersRow
AddRow = MasterDataSet.CUsers.NewCUsersRow
AddRow.UserFirst = "New"
AddRow.UserLast = "User"
AddRow.UserEmail = "Test#Email.com"
AddRow.UserPhone = "555-555-5555"
AddRow.UserWorkCenter = 482126468
AddRow.UserRole = 908977894
AddRow.MergeID = 739809237
MasterDataSet.CUsers.Rows.Add(AddRow)
If I loop through Data Here, it shows the new row
For Each NewRow As DataRow In CUsersTableAdapter.GetData()
Logger.WriteLog(NewRow.Item("UserFirst"))
Next
Try
Validate()
CUsersTableAdapter.Update(MasterDataSet.CUsers)
Catch ex As Exception
Logger.WriteLog("Failed : " & ex.Message)
End Try
The issue the whole time is the connection string was pointed to the mdf file inside the program. So nothing gets saved when running the debugger. I copied the mdf file to the desktop and changed the connection string. Everything works now so I must remember to copy the mdf to users AppData folder when installing.

Have Windows Authentication use default login page instead of popup dialog in Sharepoint 2010

Is there a way to have simple windows authentication for a public facing site (anonymous viewing is enabled so as to view the login page) but insatead of it popping up the windows auth dialog, to use a login page (aspx). I saw something similar when i switched to mixed mode authentication. SharePoint has a dropdown with "windows authentication" or "forms authentication". What i need is something similar, but just the "windows authentication" option.
I've seen similar questions on SO, but they all involve creating a custom login page. The ideal solution would involve no new pages and no coding.
Is this possible?
This could be done by launching the sharepoint page's address in internet explorer, and using some pinvoke api to send keys or settext to the login box.
I fanagled this setup for a vb.net forms application. It works on my XP. I haven't tried it in Windows 7 yet, but I'm sure it needs some adjustment for it to work there.
This uses a library called WindowScraper, from here: http://www.programmersheaven.com/download/56171/download.aspx
This library has a bunch of winapi and pinvoke built in. If your assembly won't allow it (because you are using VS 2010, perhaps), saying it doesn't have a strong name, then use SharpDevelop and rebuild the solution after adding your own certificate.
Then put the dll in your application directory and add a reference.
Then add the imports:
Imports WindowScrape
Imports WindowScrape.Constants
Imports WindowScrape.Types
Finally, the code (put all this in a module or class):
Private Property PortalAddress As String = "http://myportal#somewhere.com"
Private Property logintitle As String = "Connect to myportal#somewhere.com"
Public Sub openPortal()
If My.Computer.Info.OSFullName = "Microsoft Windows XP Professional" then
LoginToPortalXP()
Else
msgbox("Someday, we will be able to log you in automatically" & vbCr & "But it isn't ready yet.")
End If
End Sub
Private Function IsWindowReady(Optional ByVal timeout As integer = 10000)
Dim isready As Boolean = false
Dim timer As Integer = 1000
Do Until Not loginBox is nothing or timer = timeout
Thread.Sleep(1000)
loginbox = HwndObject.GetWindowByTitle(logintitle)
timer = timer + 1000
loop
If Not loginbox is nothing then isready = true
Return isready
End Function
Sub LoginToPortalXP()
Try
Dim TheBrowser As Object = CreateObject("InternetExplorer.Application")
TheBrowser.Visible = True
TheBrowser.Navigate(PortalAddress)
If Not IsWindowReady then debug.print("failed") : Exit sub
Dim sys As HwndObject = loginbox.GetChildren(1) 'SysCredential
sys.GetChildren(1).Text = "myUserName" 'username box
Thread.Sleep(500)
sys.GetChildren(4).Text = "myPassword" 'password box
Thread.Sleep(500)
loginbox.GetChildren(2).Click() 'push the okay button
Catch ex As Exception
msgbox("ERROR AutoLogging into Portal: " & vbcr & & ex.Message)
Finally
End Try
End Sub
I added the timer just in case it takes longer. You can change the timeout, of course.

Run console hidden

I'm trying to run a console window hidden while using CreateProcess(I can't use the ProcessStartInfo class because I have to run it with some other special settings)
I have tried to use the CREATE_NO_WINDOWS flag, but somehow, the console still pops up. This is the code I have:
Dim ProzessInfo = New Process_Information
Dim StartInfo = New Startup_Information, PS = New Security_Flags, TS = New Security_Flags
If CreateProcess(Nothing, target, PS, TS, False, PROCESS_CREATION_FLAG.CREATE_NO_WINDOW, Nothing, Nothing, StartInfo, ProzessInfo) = 0 Then MsgBox("Couln't start application")
What have I missed to run it hidden?
You may want to try
AppwinStyle.Hide, True
MSDN AppWinStyle
Or
EDIT:
Try This for Processes
Dim psi1 As New ProcessStartInfo("file path here")
Process.CreateNoWindow = True
Depending on what your end goal is you can always change the application type to Windows Forms Application. (Assuming you are running a console app now.)