I am writing a desktop application and in one part I go through OAuth 2.0 flow.
The steps are:
- Start process - open web browser with login page.
- User logs in and authorize my app.
- In a background I search for process with a specific name, save it (name) in app.
- At the end I close the web browser.
The problem is that if user had previously opened some tabs in web browser - in point 1. new tab is added and in point 4. everything is closed (web browser with all tabs).
Can you tell me if there is a way to close a single tab in web browser? Or maybe there is other/easier way to go through OAuth?
As a manual solution I will simply show info to the user “Now you can close this tab”, however I would like to do it automatically.
This is C# .Net 4.0 WPF project.
string strAuthUrl = "http://accounts.example.com/login",
strAuthCode = string.Empty;
// Request authorization from the user (by opening a browser window):
ProcessStartInfo startInfo = new ProcessStartInfo(strAuthUrl);
startInfo.CreateNoWindow = false;
//1.
Process.Start(startInfo);
//2. - is happening in web browser
//3.
do
{
foreach (Process proc in Process.GetProcesses())
{
if (proc.MainWindowTitle.StartsWith("Success code="))
{
strAuthCode = proc.MainWindowTitle.ToString().Substring(13, 30);
//4.
try
{
// Close process by sending a close message to its main window.
proc.CloseMainWindow();
// Free resources associated with process.
proc.Close();
// Wait 500 milisecs for exit
proc.WaitForExit(500);
//if proc has not exited so far - kill it
if (proc.HasExited == false)
{
proc.Kill();
proc.WaitForExit();
}
}
catch (Exception ex)
{
//Do something with exception
}
break;
}
}
}
while (string.IsNullOrEmpty(strAuthCode));
Thanks for your suggestions.