Search code examples
c#google-oauthhttplistener

HttpListener keep waiting forever if the localhost connection in closed from the browser


I am trying to integrate Google Auth in a native desktop application using a loopback approach by referring to Google OAuthDesktopApp. The problem that I am running into is httplistner keep waiting forever if the user has closed the tab in the browser. Is there a way by which listener can give a call back if the user has a closed browser tab? or any other approach which helps me to overcome this deadlock situation.


Solution

  • You could consider creating a new form using the WebBrowser component: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.webbrowser?view=netframework-4.8

    Or if you're doing things in WPF: https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.webbrowser?view=netframework-4.8

    What I typically do when taking this approach is create a new form with a docked WebBrowser in it, which not only allows me to detect if the window is closed, I can also capture the redirect in those instances when the vendor won't allow a custom redirect URI.

    The biggest disadvantage to this approach is that the built in browser is pretty awful; however, for a simple OAuth flow it is normally good enough.