|
| 1 | +using IdentityModel.OidcClient; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Net; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +Console.WriteLine("+-----------------------+"); |
| 8 | +Console.WriteLine("| Sign in with OIDC |"); |
| 9 | +Console.WriteLine("+-----------------------+"); |
| 10 | +Console.WriteLine(""); |
| 11 | +Console.WriteLine("Press any key to sign in..."); |
| 12 | +Console.ReadKey(); |
| 13 | + |
| 14 | +SignIn(); |
| 15 | + |
| 16 | +Console.ReadKey(); |
| 17 | + |
| 18 | +async void SignIn() |
| 19 | +{ |
| 20 | + // create a redirect URI using an available port on the loopback address. |
| 21 | + string redirectUri = string.Format("http://127.0.0.1:7890/"); |
| 22 | + Console.WriteLine("redirect URI: " + redirectUri); |
| 23 | + |
| 24 | + // create an HttpListener to listen for requests on that redirect URI. |
| 25 | + var http = new HttpListener(); |
| 26 | + http.Prefixes.Add(redirectUri); |
| 27 | + Console.WriteLine("Listening.."); |
| 28 | + http.Start(); |
| 29 | + |
| 30 | + var options = new OidcClientOptions |
| 31 | + { |
| 32 | + Authority = "https://demo.duendesoftware.com", |
| 33 | + ClientId = "interactive.public", |
| 34 | + Scope = "openid profile api", |
| 35 | + RedirectUri = redirectUri, |
| 36 | + }; |
| 37 | + |
| 38 | + var client = new OidcClient(options); |
| 39 | + var state = await client.PrepareLoginAsync(); |
| 40 | + |
| 41 | + if(state.IsError) |
| 42 | + { |
| 43 | + Console.WriteLine($"Failed to create authentication state: {state.Error} - {state.ErrorDescription}"); |
| 44 | + http.Stop(); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + Console.WriteLine($"Start URL: {state.StartUrl}"); |
| 49 | + |
| 50 | + // open system browser to start authentication |
| 51 | + Process.Start(new ProcessStartInfo |
| 52 | + { |
| 53 | + FileName = state.StartUrl, |
| 54 | + UseShellExecute = true, |
| 55 | + }); |
| 56 | + |
| 57 | + // wait for the authorization response. |
| 58 | + var context = await http.GetContextAsync(); |
| 59 | + |
| 60 | + // sends an HTTP response to the browser. |
| 61 | + var response = context.Response; |
| 62 | + string responseString = string.Format("<html><head><meta http-equiv='refresh' content='10;url=https://demo.duendesoftware.com'></head><body>Please return to the app.</body></html>"); |
| 63 | + var buffer = Encoding.UTF8.GetBytes(responseString); |
| 64 | + response.ContentLength64 = buffer.Length; |
| 65 | + var responseOutput = response.OutputStream; |
| 66 | + await responseOutput.WriteAsync(buffer, 0, buffer.Length); |
| 67 | + responseOutput.Close(); |
| 68 | + |
| 69 | + var result = await client.ProcessResponseAsync(context.Request.RawUrl, state); |
| 70 | + |
| 71 | + BringConsoleToFront(); |
| 72 | + |
| 73 | + if (result.IsError) |
| 74 | + { |
| 75 | + Console.WriteLine("\n\nError:\n{0}", result.Error); |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + Console.WriteLine("\n\nClaims:"); |
| 80 | + foreach (var claim in result.User.Claims) |
| 81 | + { |
| 82 | + Console.WriteLine("{0}: {1}", claim.Type, claim.Value); |
| 83 | + } |
| 84 | + |
| 85 | + Console.WriteLine(); |
| 86 | + Console.WriteLine("Access token:\n{0}", result.AccessToken); |
| 87 | + |
| 88 | + if (!string.IsNullOrWhiteSpace(result.RefreshToken)) |
| 89 | + { |
| 90 | + Console.WriteLine("Refresh token:\n{0}", result.RefreshToken); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + http.Stop(); |
| 95 | +} |
| 96 | + |
| 97 | +// Hack to bring the Console window to front. |
| 98 | +// ref: http://stackoverflow.com/a/12066376 |
| 99 | +[DllImport("kernel32.dll", ExactSpelling = true)] |
| 100 | +static extern IntPtr GetConsoleWindow(); |
| 101 | + |
| 102 | +[DllImport("user32.dll")] |
| 103 | +[return: MarshalAs(UnmanagedType.Bool)] |
| 104 | +static extern bool SetForegroundWindow(IntPtr hWnd); |
| 105 | + |
| 106 | +void BringConsoleToFront() |
| 107 | +{ |
| 108 | + SetForegroundWindow(GetConsoleWindow()); |
| 109 | +} |
0 commit comments