locked
Problem with process not ending RRS feed

  • Question

  • User-1458727574 posted

    I have some code in a method. What it does is to start up a browser session which takes the user to a site to get them to log in and provide authorisation. It's using OAuth, but the issue isn't with OAuth itself. The problem is that my code starts up the browser and then waits for a response back from the browser session.

                HttpListener http = new HttpListener();
                http.Prefixes.Add(appSettings.RedirectUrl);
                http.Start();
                System.Diagnostics.Process.Start(url);
                HttpListenerContext context = http.GetContext();
                HttpListenerResponse response = context.Response;
                string responseString = string.Format("<html><head><meta http-equiv='refresh' content='5;url=http://www.google.com'></head><body>Redirecting...</body></html>");
                Activate();
    
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
                response.ContentLength64 = buffer.Length;
                Stream responseOutput = response.OutputStream;
    
                Task responseTask = responseOutput.WriteAsync(buffer, 0, buffer.Length).ContinueWith((task) =>
                {
                    responseOutput.Close();
                    http.Stop();
                });
    

    The code after this reads the response and normally everything is fine. However, if at the start of this code, the user does not log into the site to go through OAuth and simply closes the browser, my httplistener will just sit and wait there forever. I don't want to put a time limit on it because I can't guarantee how long it will take the user to go through the OAuth process, but I need a way of detecting that the browser has closed and shutting down the listener. Another issue which may mean this isn't possible is that if the user instead of closing the browser, still doesn't go through the OAuth, but navigates away from the site, the browser session will remain open. It means that my code never exits.

    Any ideas of what I can do to close the listener off? I think it has to have a timeout, but I don't know the code needed to do that. Any help would be cool.

    Thanks

    Friday, May 1, 2020 1:13 PM

Answers

All replies

  • User475983607 posted

    Can you explain the high level problem you are trying to solve?  Wouldn't a link to a secured resource accomplish the same?

    AnyUserNameThatLetsMeIn

    However, if at the start of this code, the user does not log into the site to go through OAuth and simply closes the browser, my httplistener will just sit and wait there forever. I don't want to put a time limit on it because I can't guarantee how long it will take the user to go through the OAuth process, but I need a way of detecting that the browser has closed and shutting down the listener.

    Set a timer.

    Friday, May 1, 2020 1:26 PM
  • User-1458727574 posted

    Can you explain the high level problem you are trying to solve?  Wouldn't a link to a secured resource accomplish the same?

    AnyUserNameThatLetsMeIn

    However, if at the start of this code, the user does not log into the site to go through OAuth and simply closes the browser, my httplistener will just sit and wait there forever. I don't want to put a time limit on it because I can't guarantee how long it will take the user to go through the OAuth process, but I need a way of detecting that the browser has closed and shutting down the listener.

    Set a timer.

    If the user starts the process and goes to the site to start the OAuth journey, there is absolutely nothing stopping them from navigating away before they complete, or just closing the browser before the response comes back. I can't physically force them to go through the OAuth journey. At the moment, the code waits forever, which is not ideal. So, you say set a timer. I thought it would be that, but I wasn't sure if there were alternative options.

    So, setting a timer. I wasn't sure how to set a timer that killed the process off after a set period of time. Can you help a bit with that? I've used timers before but not when spawning a process off to kill it after a period of time.

    Friday, May 1, 2020 1:46 PM
  • User475983607 posted

    The HttpListener has a timeout manager.  You can use timeout manager or just set a timer that closes the listener when the timer event fires.  The official timer docs illustrate the pattern.  It's pretty simple.  You can pretty much copy and paste then update the method bodies.

    https://docs.microsoft.com/en-us/dotnet/api/system.timers.timer?view=netcore-3.1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 1, 2020 2:34 PM
  • User-1458727574 posted

    I added a timer to the application in the end and set it to automatically exit with a different exit code when the timer fires. If they complete the process before the timer fires, the application will automatically exit anyway with a normal exit code. Thanks

    Monday, May 4, 2020 12:05 PM