private static async Task <AuthResult> PollinationSignInAsync(bool devEnv = false) { if (!HttpListener.IsSupported) { Helper.Logger.Error($"PollinationSignInAsync: HttpListener is not supported on this system"); throw new ArgumentException("PollinationSignIn is not supported on this system"); } var redirectUrl = "http://localhost:8645/"; var loginUrl = devEnv ? LoginURL_Dev : LoginURL; var listener = new System.Net.HttpListener(); try { listener.Prefixes.Add(redirectUrl); listener.Start(); //listener.TimeoutManager.IdleConnection = TimeSpan.FromSeconds(30); //listener.TimeoutManager.HeaderWait = TimeSpan.FromSeconds(30); } catch (HttpListenerException e) { //it is already listening the port, but users didn't login if (e.ErrorCode == 183) { Console.WriteLine(e.Message); Helper.Logger.Warning($"PollinationSignInAsync: it is still waiting for users to login from last time.\n{e.Message}"); } else { Helper.Logger.Error($"PollinationSignInAsync: Failed to start the listener.\n{e.Message}"); throw e; } } var psi = new System.Diagnostics.ProcessStartInfo { FileName = loginUrl, UseShellExecute = true }; System.Diagnostics.Process.Start(psi); Helper.Logger.Information($"PollinationSignInAsync: login from {loginUrl}"); // wait for the authorization response. var context = await listener.GetContextAsync(); var request = context.Request; var response = context.Response; var returnUrl = request.RawUrl.Contains("?token=") ? request.RawUrl : request.UrlReferrer?.PathAndQuery; if (string.IsNullOrEmpty(returnUrl)) { Helper.Logger.Error($"PollinationSignInAsync: Failed to authorize the login: \n{request.RawUrl}"); throw new ArgumentException($"Failed to authorize the login: \n{request.RawUrl}"); } //sends an HTTP response to the browser. string responseString = string.Format("<html><head></head><body style=\"text-align: center; font-family: Lato, Helvetica, Arial, sans-serif\"><img src=\"https://app.pollination.cloud/logo.svg\"><h1>Authorization was successful!</h1><p>You can close this browser window.</p></body></html>"); var buffer = Encoding.UTF8.GetBytes(responseString); response.ContentLength64 = buffer.Length; var responseOutput = response.OutputStream; await responseOutput.WriteAsync(buffer, 0, buffer.Length); await Task.Delay(1000); responseOutput.Flush(); responseOutput.Close(); listener.Stop(); Helper.Logger.Information($"PollinationSignInAsync: closing the listener"); return(AuthResult.From(request.QueryString)); }