Is there an example of how to create a dotnet core 3 console app that authenticates with IdentityServer4?

Viewed 1333

Is there a way to create a dotnet core 3/3.1 console application that authenticates with my IdentityServer4 using Open ID Connect? There is an old example in IdentityModel.OidcClient called NetCoreConsoleClient, but it requires ASP.Net Core.

The model that I'm looking for is that a user attempts to use the console app, the console app pops open a browser window and initiates an Open ID Connect session with IdentityServer4, the user enters username/password in the browser window, control returns to the console app which now has the necessary tokens to talk to a protected API server. In Microsoft's azure command line tools, they offload authentication to something that seems like OpenID connect. I'm looking to do something similar.

Is ASP.Net Core somehow required for that flow? I don't see why it would be.

Is this the Authorization Code grant type, and if so, how are the tokens passed back to the console app without using a callback URL? We don't want to have to have an open public IP port necessary just to run a console app.

1 Answers

Here are a couple of examples that use IdentityModel libraries and demonstrate techniques you need. If coding in .Net I would recommend their libraries - they have versions for both .Net Core and the older .Net Framework:

In both cases you use Authorization Code Flow (PKCE) and tokens are passed back to the app slightly differently. The console app will receive an authorization code when login completes and can then swap the code for tokens.

LOOPBACK

During logins the console app temporarily listens for the login response on an address such as http://localhost:8000, which does not require administrator rights.

PRIVATE URI SCHEMES

During installation your scheme, such as x-mycompany-myconsoleapp, is registered as a per user setting. The app receives the login response via an operating system notification to a scheme based URL.

FURTHER INFO

Out of interest, the OAuth flow options for console apps are the same as for desktop apps. Some blog posts of mine may give you an idea of Open Id connect messages and other related behaviour:

Related