ASP.NET Core (API, Blazor, MVC) + Keycloak — Part 3

Luy Lucas
2 min readJan 28, 2024

--

In this part, we’ll integrate a MVC app with Keycloak

Part 1;

Part 2;

Part 4;

Bonus;

For the MVC app, we do almost the same thing like on API before.

We have to install this package:

Install-Package Microsoft.AspNetCore.Authentication.OpenIdConnect

Copy the keycloak section from the API appsettings, we can use the same client:

Copy the applicationUrl from launchSettings and input as Valid Redirect URI in the Keycloak client:

I used the same client for all examples in this series

In the program.cs file we write these configurations:

Don’t forget to activate the Auth and Authz middlewares.

In the HomeController.cs We can use the [Authorize] attribute in the Privacy action.

Run this and click on the privacy link, You’ll be redirected to Keycloak login page, use the usertest1 credentials. Create a breakpoint in the Index or Privacy action and see the User:

Well that’s all for now!

Next, I’ll share Blazor WASM solution to this.

Cya.

--

--