Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit c99f19b

Browse files
committed
Clean up test clients
- Remove defunct ConsoleClient - Add a manual mode console client
1 parent 0a22f86 commit c99f19b

File tree

6 files changed

+153
-192
lines changed

6 files changed

+153
-192
lines changed

.vscode/launch.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@
3737
"ASPNETCORE_ENVIRONMENT": "Development"
3838
},
3939
"console": "externalTerminal"
40-
}
40+
},
41+
{
42+
"name": "ManualModeConsoleClient",
43+
"type": "coreclr",
44+
"request": "launch",
45+
"preLaunchTask": "build-ManualModeConsoleClient",
46+
"program": "${workspaceFolder}/clients/ManualModeConsoleClient/bin/Debug/net8.0-windows/ManualModeConsoleClient.dll",
47+
"args": [],
48+
"cwd": "${workspaceFolder}/clients/ManualModeConsoleClient",
49+
"serverReadyAction": {
50+
"action": "openExternally",
51+
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
52+
},
53+
"env": {
54+
"ASPNETCORE_ENVIRONMENT": "Development"
55+
},
56+
"console": "externalTerminal"
57+
},
4158
]
4259
}

.vscode/tasks.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
"/consoleloggerparameters:NoSummary"
2525
],
2626
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "build-ManualModeConsoleClient",
30+
"type": "process",
31+
"command": "dotnet",
32+
"args": [
33+
"build",
34+
"${workspaceFolder}/clients/ManualModeConsoleClient/ManualModeConsoleClient.csproj",
35+
"/property:GenerateFullPaths=true",
36+
"/consoleloggerparameters:NoSummary"
37+
],
38+
"problemMatcher": "$msCompile"
2739
}
2840
]
2941
}

clients/ConsoleClient/ConsoleClient.csproj

Lines changed: 0 additions & 18 deletions
This file was deleted.

clients/ConsoleClient/Program.cs

Lines changed: 0 additions & 173 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0-windows</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\..\src\OidcClient\OidcClient.csproj" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)