Prevent user from closing Firefox - c#

I absolutely need a user to log out of a website which she/he uses to access our database. If one doesn't log out, and simply closes the browser, the system locks the username for an hour. I did not implement it, it's just the way it works.
I thought to write a simple C# program that would somehow detect whether the user logged out, and if not, prevent them from closing the browser.
1) Is there Firefox API, or any other way to read the website content in firefox.exe process?
2) When a user hits 'X' to close the browser, is it possible to abort the termination of firefox.exe process? (probably this is the deal-breaker question).
I would appreciate any hints. Thanks!

Every user should have a session, if you are using authentication. Sessions can be configured to expire. All you need to do is just to handle an appropriate session expire event.
Do not look for any hack to handle a browser exit event - it is a dangerous path:
Everybody would hate it, and there is no chance you can make it reliable, i.e. working in newer versions of browsers, supporting many browsers on different OS etc
This may work in Firefox Mozilla (sample), but may not work in IE or Chrome
Application can be killed by OS, so browser events will not get a chance to fire and your handling code will not work

You can't stop the user from closing firefox, because there a lot of ways it can be closed that can't be controlled by your code (e.g. killing the process from taskmon). However, you can detect the closure event in your code (window.onClose() event) and do the log out process.
However, in case of firefox (or any other browser for that matter), gets killed rather than being closed, window.onClose() will not work. So its better to handle the session in the server rather than depending on the client behavior.

What you can do is use javascript to detect the browser closing or leaving your site and in those instances fire a method or hit a webservice that logs them out. Very quick, very simple and will work across browsers.

You cannot prevent a user from closing a program with legitimate methods. This is for obvious security purposes.

I agree with oleksii. But you can make it less easy for users to close Firefox.
I'm currently writing an add-on (via the SDK) that listens to the "quit-application-requested" Observer Notification and sets aSubject.data to true preventing Firefox from quittng the application.
observe: function(aSubject, aTopic, aData) {
if (aTopic == "quit-application-requested") {
aSubject.QueryInterface(Ci.nsISupportsPRBool);
aSubject.data = (this.canQuit() == false);
}
}
But what I actually want is a certain window not being closed, as to not close the web application (a FileMaker database).
Therefore I've also tried disabling all commands, all keys and all menus (browser XUL), like so:
var allCommands = win.document.getElementsByTagName("command");
for (let i = 0; i < allCommands.length; i++) {
allCommands[i].setAttribute("disabled", "true");
}
var allKeys = win.document.getElementsByTagName("key");
for (let i = 0; i < allKeys.length; i++) {
allKeys[i].setAttribute("disabled", "true");
}
var allMenus = win.document.getElementsByTagName("menu");
for (let i = 0; i < allMenus.length; i++) {
allMenus[i].setAttribute("disabled", "true");
}
But users can still quit the application and thus close the window with the web application unless I prevent opening new Firefox windows.
So "it" works but I would like for them to still browse and just not close a specific tab or window in which the tab resides.
Without preventing opening of new windows it doesn't work because apparently when a user opens a new window then when users press Alt+F4 it would not be an application quit but a window close. Although I thought I had disabled all commands/keys/menus, still there exists some kind of close possibility.
Does anyone have any ideas on how to accomplish that without preventing the opening of new windows?

Related

Process.Exited raising immediately after Process.Start [duplicate]

i have a strange problem with IE8 installed in xp. i was trying to launch IE using an System.Diagnostics.Process.Start method in c#. And i have a requirement to trap the exited event of the IE and do some operation. But i ended up in a rather strange problem where the IE immediately fires the exited event after launch.
this is the sample code
Process objProcess = Process.Start("IEXPLORE.EXE", "http://google.com");
if (objProcess != null)
{
objProcess.EnableRaisingEvents = true;
objProcess.Exited += new EventHandler(myProcess_Exited);
}
public static void myProcess_Exited(object sender, System.EventArgs e)
{
MessageBox.Show("You exited");
}
But the above code perfectly works when laucnching different process (ex:notepad) and it fires the exit event when i close the exe.
this only gives problem launching IE 8. Can someone clarify me what is the problem??
UPDATE
Most friends replied my post and saying why you can't just use an URL? why stick with IE?
here the reason
the ultimate aim of the app is to launch an URL from the windows application and will hide an exe when working on the IE. And show the exe after closing the IE.
Thanks
Most probably is that you have IE already running as a process, so when you try to launch it again as a new process it looks that there are IE running already, tells it that user initiated a new window (so the initial IE will create a "new" window rather than a new one) and exit.
Possible solution:
try starting the process with "-nomerge" command line option:
Process objProcess = Process.Start("IEXPLORE.EXE", "-nomerge http://google.com/");
Interesting observation: objProcess.ExitCode (for IE8 at least) will be equal to 0 if exited passing control to another instance, and 1 if it was actually closed by user.
If another instance of iexplore.exe is already running on the machine, new instances will connect to that and immediately exit. Also, it's possible that even in the case where iexplore is not running, the multiprocess architecture of Internet Explorer 8 has the parent launch child broker process and exit immediately.
But these answers are besides the point. You should not be launching Internet Explorer directly. If the user has configured another default browser, they will be unhappy that you are ignoring their preferences. Instead, why don't you try
System.Diagnostics.Process.Start("http://google.com");
directly and that will do the right thing. You won't be able to tell when the browser closed, but if the command has opened a new tab in an existing browser session for example, the browser close event will be meaningless to your application.
Maybe IEXPLORE itself launches a different process for the URL and ends the process you created? Like a fork on Unix?

IE Windows Security dialog appears to block Selenium navigation

I am using Selenium in C# on Windows 10. The site under test should challenge with a Windows Security login box in IE, which it does. But the login box appears to block the call.
var home = "https://site.under.test.com/";
driver = new InternetExplorerDriver(AppDomain.CurrentDomain.BaseDirectory);
driver.Url = home;
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl(home + "secure/");
//code to handle login box goes here, never gets executed unless the dialog box
//is manually addressed or something times out in GoToURL(),
//and then the dialog box doesn't work.
The login box appears:
Click here for screenie of the login box
But the execution is stalled on the GoToUrl() call:
Click here for screenie of execution
Doesn't matter what code I place after this to handle the popup, execution is blocked until something times out inside GoToUrl().
Is this expected behavior? How does one get around it?
Clarification: The problem is not how to enter data into the popup. It is about the code execution not advancing to the point where I can enter data into the popup without intervention or timeout.
This works on Win8.1, but not on Win10
The Webdriver no longer officially supports Basic Authentication. It has been removed from the Java code and the .Net version works accidentally. From my experience you can expect it to work (for now) on Win7-8.1, but not on Win10
This from Selenium support:
The support of authentication dialogs was removed from the java bindings with this commit.
I am surprised, they are still present in the c# binding, because there is no support for basic authentication in the webdriver specification.
Not sure if you found the answer yet but here is what I found so far.
.GoToUrl waits for the page to load and since the page has not loaded yet, it waits and waits and throws exception.
You can enter the url usign Javascript. Below for is .Net code for it
string script = "window.location = \'" + url + "\'";
((IJavaScriptExecutor)driver).ExecuteScript(script);
You have to manage your own wait after using the above code.
And Selenium still doesnt work with the Windows Security dialog that you see after this (Or, I havent found any info anywhere that shows how to make Selenium work with this dialog)
The best solution I have found so far is to use AutoIT, and use TAB commands within AutoIT to enter username, move to password adn enter password and then do tab tab until you get to submit button and then click on this location. Please comment here if you have found any better solution than this until now

How to detect when an internet-browser is open in Windows Forms and do an action if an URL was fired?

I want to write a program which recognizes when a browser is open and which do every time an action, when the user went to a website. For example:
The program is running as a system tray and starting automatically on windows startup. (this works)
Now the program runs an function, if the client open a random internet-browser (IE, Chrome, ...) which have the example-code MessageBox.Show("You opened a browser!").
If the user types for example "www.google.com" in the address bar and push [enter] the program should open an example-function like MessageBox.Show("You entered " + enteredURL) before the Website is loaded.
Thanks in advance for your help!
Take a look at the Navigating event:
Occurs before the WebBrowser control navigates to a new document.
It is possible to connect to an existing instance of IE, but you'll need to work with underlying the COM API (see here).
For other browsers there is no general mechanism: you'll need to work out if some API even exists browser by browser.

Exit(Quit) implementation

In my windows phone7(Silverlight) Application I have to display a message box asking, the user to confirm(Yes/No) before exit from the application [on device back button click].
The problem is I have to use a custom messagebox(using a popup) to get user confirmation, and I have no way to get exit from the application.(No method found which will exit the application like dispose() or close()).
if I didn't have to use a custom messagebox, the on the Device back key press event "OnBackKeyPress" I would have use the following logic and done my work
MessageBoxResult res = MessageBox.Show("Do you want to Exit?", "Exit", MessageBoxButton.OKCancel);
if (res == MessageBoxResult.OK)
{
if (NavigationService.CanGoBack)
{
while (NavigationService.RemoveBackEntry() != null)
{
NavigationService.RemoveBackEntry();
}
}
}
else
{
e.Cancel = true;
}
The problem is I need to use the custom messagebox and done this work. Same problem arise if need to implement a button to exit the application with out using the device back button.
I found in several posts suggesting to throw an exception and make this done. Following are some of them
http://mobile.dzone.com/articles/windows-phone-mango-sample-3
http://imaginativeuniversal.com/blog/post/2010/08/22/How-to-Quit-a-WP7-Silverlight-Application.aspx
I don't think that this is a good practice and also not sure if the windows market place will certify this way. Would like to hear the thoughts of once who have experienced this issue, and any suggestion to Achieve this(Terminate the application). Thanks inadvance....!!!!
If you want to submit to the Marketplace you've got a couple of problems because of the following certification requirements:
5.2.4.2 Pressing the Back button from the first screen of an application must close the application.
5.2.2 A Windows Phone application is closed and terminated by the OS when the user navigates away from the application. When an application is started after being closed, its launch time must meet the requirements in Section 5.2.1 – Launch Time
5.2.3 A Windows Phone application is deactivated when the user presses the Start button or if the device timeout causes the lock screen to engage. A Windows Phone application is also deactivated with it invokes a Launcher or Chooser API.
This is a couple instances where you simply can't display a message box.
And technically using an exception to termniate the app is a violation:
5.1.2 The application must handle exceptions raised by the .NET Framework and not close unexpectedly. During the certification process, the application is monitored for unexpected closure. An application that closes unexpectedly fails certification. The application must continue to run and remain responsive to user input after the exception is handled.
As a developer, part of your job is communicating to users about requirements that are unrealistic or unreasonable.
Sorry, no way to do this. Before mango update you could Clear the back stack, then programmatically trigger the Back button. but as of SDK 7.1 (wp7.5) we can no longer do this.
My recommendation is to create a custom Exception type ApplicationXExitException and throw that to exit the app. The reason for the custom type is so that when you pull your exception logs from the marketplace, you'll know the ones that were indeed unintended exceptions crashing the app, vs your exception to intentionally exit the app.
You can hook an event raising after your custom messagebox closes. Event arguments will keep information about user's choice. Depending on that you will decide whether to exit app or not.

Process.Start("IEXPLORE.EXE") immediately fires the Exited event after launch.. why?

i have a strange problem with IE8 installed in xp. i was trying to launch IE using an System.Diagnostics.Process.Start method in c#. And i have a requirement to trap the exited event of the IE and do some operation. But i ended up in a rather strange problem where the IE immediately fires the exited event after launch.
this is the sample code
Process objProcess = Process.Start("IEXPLORE.EXE", "http://google.com");
if (objProcess != null)
{
objProcess.EnableRaisingEvents = true;
objProcess.Exited += new EventHandler(myProcess_Exited);
}
public static void myProcess_Exited(object sender, System.EventArgs e)
{
MessageBox.Show("You exited");
}
But the above code perfectly works when laucnching different process (ex:notepad) and it fires the exit event when i close the exe.
this only gives problem launching IE 8. Can someone clarify me what is the problem??
UPDATE
Most friends replied my post and saying why you can't just use an URL? why stick with IE?
here the reason
the ultimate aim of the app is to launch an URL from the windows application and will hide an exe when working on the IE. And show the exe after closing the IE.
Thanks
Most probably is that you have IE already running as a process, so when you try to launch it again as a new process it looks that there are IE running already, tells it that user initiated a new window (so the initial IE will create a "new" window rather than a new one) and exit.
Possible solution:
try starting the process with "-nomerge" command line option:
Process objProcess = Process.Start("IEXPLORE.EXE", "-nomerge http://google.com/");
Interesting observation: objProcess.ExitCode (for IE8 at least) will be equal to 0 if exited passing control to another instance, and 1 if it was actually closed by user.
If another instance of iexplore.exe is already running on the machine, new instances will connect to that and immediately exit. Also, it's possible that even in the case where iexplore is not running, the multiprocess architecture of Internet Explorer 8 has the parent launch child broker process and exit immediately.
But these answers are besides the point. You should not be launching Internet Explorer directly. If the user has configured another default browser, they will be unhappy that you are ignoring their preferences. Instead, why don't you try
System.Diagnostics.Process.Start("http://google.com");
directly and that will do the right thing. You won't be able to tell when the browser closed, but if the command has opened a new tab in an existing browser session for example, the browser close event will be meaningless to your application.
Maybe IEXPLORE itself launches a different process for the URL and ends the process you created? Like a fork on Unix?

Categories

Resources