1

I'm implementing a OAUTH-type of authentication for a desktop application and in this process I need to open a webbrowser and then close it when token has been collected.

I've tried with this:

var p = Process.Start(new ProcessStartInfo(url));
...
p.Kill(); //this won't work as p is null = no new process started

According to the documentation:

If the process is already running, no additional process resource is started. Instead, the existing process resource is reused and no new Process component is created. In such a case, instead of returning a new Process component, Start returns null to the calling procedure.

Indeed, the page is just opened as a new tab in Chrome (my default browser) process that I've already have opened.

Any ideas on how to open a new process of the default browser that I can kill?

Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
Niels Bosma
  • 11,758
  • 29
  • 89
  • 148
  • I've heard of WebDriver? Would that be useful? –  Feb 20 '15 at 07:13
  • 3
    Is this an interactive app? If so when not use the web browser control within your own app? (Even if you can always open a new browser instance, the user could open new tabs which disappear if you kill it.) – Richard Feb 20 '15 at 07:13
  • I've tried to use the WebBrowser control but that renders in IE7 mode (and I can't fix that using the know registry hack as I'm not allowed to change the registry) and the auth page won't render in IE7. – Niels Bosma Feb 20 '15 at 07:18

2 Answers2

4

The main problem here is, that the second process will be opened, Chrome notices that one instance is already open, so it kills your process and adds it to you already opened process. When you try to kill the process, its already closed. But I think you already sorted that out.

What I found is this question, it could help you, but I haven't tested it, it might be using the same process aswell, but anyway, try it out:

https://superuser.com/questions/213460/how-can-you-configure-chrome-to-open-new-browser-instances-in-new-windows-rather

Community
  • 1
  • 1
Jannik
  • 2,310
  • 6
  • 32
  • 61
  • I need a browser agnostic solution though. – Niels Bosma Feb 20 '15 at 08:05
  • Every browser handles their process starts differently. You wont find a generic Method, but you could try to implement you own browser or just implementiert it for a selection of browsers. – Jannik Feb 20 '15 at 12:57
2

I have written a small function to open a webrowser using SHDocVw.

you will need to add a reference to a COM component called Microsoft Internet Controls. you can see it here

it enables you to manipulate the window as you want. (the Task.Run is only to open it in a new thread so you can remove it if you want)

when you done you can call ie.Quit()

public void OpenKioskBrowser(string URL)
{
    Task.Run(() =>
    {
        SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
        ie.Navigate(URL);
        ie.ToolBar = 0;
        ie.AddressBar = false;
        ie.Width = 350;
        ie.Height = 200;
        ie.Visible = true;
    });
}

another example of usage (Console application):

static void Main(string[] args)
{
    SHDocVw.InternetExplorer browserObj = new SHDocVw.InternetExplorer();
    browserObj.Navigate("http://stackoverflow.com");
    browserObj.ToolBar = 1;
    browserObj.AddressBar = true;
    browserObj.FullScreen = true;
    browserObj.Visible = true;

    Thread.Sleep(5000);

    browserObj.Quit();
}
Community
  • 1
  • 1
Sagiv b.g
  • 30,379
  • 9
  • 68
  • 99