<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[SSO in ASP.NET Core MVC with Keycloak]]></title><description><![CDATA[SSO in ASP.NET Core MVC with Keycloak]]></description><link>https://sso-in-aspnet-core-mvc-with-keycloak.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 20 Jun 2026 04:16:58 GMT</lastBuildDate><atom:link href="https://sso-in-aspnet-core-mvc-with-keycloak.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to Secure Your ASP.NET MVC App with Keycloak and OpenID Connect?]]></title><description><![CDATA[In this walkthrough, you’ll learn how to integrate Keycloak with an ASP.NET Core MVC application to enable Single Sign-On (SSO). This setup allows your app to delegate authentication to Keycloak securely, using OpenID Connect.
Prerequisite

Installat...]]></description><link>https://sso-in-aspnet-core-mvc-with-keycloak.hashnode.dev/how-to-secure-your-aspnet-mvc-app-with-keycloak-and-openid-connect</link><guid isPermaLink="true">https://sso-in-aspnet-core-mvc-with-keycloak.hashnode.dev/how-to-secure-your-aspnet-mvc-app-with-keycloak-and-openid-connect</guid><category><![CDATA[keycloak]]></category><category><![CDATA[IAM]]></category><category><![CDATA[SSO]]></category><category><![CDATA[.net core]]></category><category><![CDATA[Vault]]></category><category><![CDATA[TLS]]></category><dc:creator><![CDATA[0xSAF]]></dc:creator><pubDate>Wed, 10 Sep 2025 14:20:37 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1757574567720/0eb90976-87d6-43a3-8211-50d87732e9d3.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this walkthrough, you’ll learn how to integrate Keycloak with an <a target="_blank" href="http://ASP.NET">ASP.NET</a> Core MVC application to enable Single Sign-On (SSO). This setup allows your app to delegate authentication to Keycloak securely, using OpenID Connect.</p>
<p><strong>Prerequisite</strong></p>
<ol>
<li><p>Installation of Keycloak and set up the database and admin. <a target="_blank" href="https://keycloak.hashnode.dev/">Visit here.</a></p>
</li>
<li><p>ASP .NET Core MVC App. <a target="_blank" href="https://github.com/Safal08/firstmvcapp">GitHub Link to MVC App</a></p>
<ul>
<li><p><strong>.NET SDK (6/7/8)</strong> installed (<strong><em>For this walkthrough version is 6.0.400</em></strong>)</p>
</li>
<li><p><strong>Code Editor -</strong> <strong>VS Code</strong></p>
</li>
<li><p>Basic knowledge of <a target="_blank" href="http://ASP.NET"><strong>ASP.NET</strong></a> <strong>Core MVC</strong></p>
</li>
<li><p>Basic understanding of <strong>authentication and authorization</strong></p>
</li>
</ul>
</li>
</ol>
<p><strong>Why Keycloak?</strong></p>
<ul>
<li><p>Open-source identity and access management tool.</p>
</li>
<li><p>Supports SSO, OIDC, OAuth2, SAML.</p>
</li>
<li><p>Manages users, roles, and permissions.</p>
</li>
<li><p>Useful in microservices and enterprise setups.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757496602369/7c276f3b-7116-4410-9beb-a89357ad217c.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-keycloak-configuration"><strong>Keycloak configuration</strong></h2>
<ul>
<li><p>Creating a realm</p>
</li>
<li><p>Adding a client for your <a target="_blank" href="http://ASP.NET">ASP.NET</a> app</p>
</li>
</ul>
<p>Here we have current realm as myrealm.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757492876663/d8fb4f02-9f45-4487-953b-dac28af4de10.png" alt class="image--center mx-auto" /></p>
<p><strong>Go to Clients&gt;Create Client</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757492925945/d9dd46fd-fde5-4ed9-a049-3294c7cd9f6a.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757492988060/82789c60-594d-4320-8d08-5fac48a7978f.png" alt class="image--center mx-auto" /></p>
<p>In Capability config, turn on the client authentication. This is very important because it creates a secret key which we need to integrate later in our app.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757493089578/da12fc59-7fea-4962-b37e-3d16f8c8354c.png" alt class="image--center mx-auto" /></p>
<p>Follow for the Root URL, Valid redirect URIs and logout ridirect URIs. <mark>Note your .NET application might be run in different port.</mark></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757493223873/411e1f94-f98b-46fb-8885-551206af7d08.png" alt class="image--center mx-auto" /></p>
<p>After that go to the list of clients and click the newly generated client. We are interested in credentials tab.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757493321744/1f9248fb-b23f-4344-a364-83fdd91a4223.png" alt class="image--center mx-auto" /></p>
<p><strong>This is the Client Secret we need later.</strong></p>
<h2 id="heading-configuring-aspnethttpaspnet-core-mvc-app"><strong>Configuring</strong> <a target="_blank" href="http://ASP.NET"><strong>ASP.NET</strong></a> <strong>Core MVC App</strong></h2>
<p>Please install all the necessary packages from the <strong>NuGet</strong> package installer.</p>
<p>Integrate the OIDC middleware.</p>
<p>In <strong>program.cs</strong> file</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757493638505/f815dfc4-8bd0-42ff-885b-cc9f60b78b05.png" alt class="image--center mx-auto" /></p>
<p>In <strong>appsettings.json</strong> file, add the following code. <strong><mark>Note: Please careful with your realms name, client secret and client ID.</mark></strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757494103477/f50ef0f9-6443-487a-966f-7e04b38ecb63.png" alt class="image--center mx-auto" /></p>
<p>In <strong>HomeController.cs →</strong> Here we need to go through the keycloak to view the secure page.</p>
<ul>
<li><p>Our app <strong>outsources login</strong> to Keycloak.</p>
</li>
<li><p>Users don't type passwords into our app — only into Keycloak.</p>
</li>
<li><p>Once logged in, they can access protected pages in your app.</p>
</li>
</ul>
<p>This is the secure view</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757493851685/28c7f9ab-fd4c-4f1a-8de6-53709e87fe98.png" alt class="image--center mx-auto" /></p>
<p>In <strong>AccountController.cs</strong> → For login and logout action.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757494218469/a66db882-ec1f-4f93-abc1-820f81fcf4bc.png" alt class="image--center mx-auto" /></p>
<p>In <strong>Index.cshtml</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757494372123/3a48e70a-553f-44bb-b8f5-98ca493eedd4.png" alt class="image--center mx-auto" /></p>
<p>In <strong>_Layout.cshtml</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757494434436/3a8bfe2f-9e44-438e-85da-3e7b5d822e56.png" alt class="image--center mx-auto" /></p>
<p>Then start the keycloak server.</p>
<pre><code class="lang-bash">sudo ./bin/kc.sh start-dev
</code></pre>
<p>Start the .NET Application.</p>
<pre><code class="lang-bash">dotnet run
</code></pre>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://www.youtube.com/watch?v=VjFpQdvglzo">https://www.youtube.com/watch?v=VjFpQdvglzo</a></div>
<p> </p>
<p>Here we can see the demo that the user John Cena tries to log in and he redirects to the Keycloak login page and once authenticated successfully he is able to see the secure page output.</p>
<h2 id="heading-set-up-tls-using-openssl"><strong>Set up TLS using OpenSSL</strong></h2>
<p>Let’s walk through <strong>how to set up TLS using OpenSSL</strong> for your <strong>Keycloak server</strong> on <a target="_blank" href="http://localhost">localhost</a>. This will give you a <strong>self-signed TLS certificate</strong> so Keycloak can run securely over <a target="_blank" href="https://localhost"><code>https://localhost</code></a>, which is essential for modern browser security and proper SSO functionality.</p>
<p>Create a necessary folder and files.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757497233350/71361046-51b6-46d7-9caa-a9748d21b95b.png" alt /></p>
<pre><code class="lang-bash">sudo openssl req -newkey rsa:2048 -keyout /keycloak/keycloak-26.3.3/conf/certs/keycloak.key -out /keycloak/keycloak-26.3.3/conf/certs/keycloak.csr
</code></pre>
<pre><code class="lang-bash">sudo openssl x509 -req -<span class="hljs-keyword">in</span> /keycloak/keycloak-26.3.3/conf/certs/keycloak.csr -signkey /keycloak/keycloak-26.3.3/conf/certs/keycloak.key -out /keycloak/keycloak-26.3.3/conf/certs/keycloak_decrypted.crt
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757497427536/ab222097-0127-41a2-8942-1bce8ed77fa6.png" alt /></p>
<p>Certificate and key has been created and can be seen with <strong>ls command</strong>.</p>
<p>After this open the <strong>keycloak.conf</strong> file and add these text and save it.</p>
<pre><code class="lang-plaintext">http-port=8080
https-port=8443
https-certificate-file=./conf/certs/keycloak.crt 
https-certificate-key-file=./conf/certs/keycloak_decrypted.key 
proxy-address-forwarding=true
</code></pre>
<p>Note: Make sure run the server with the <strong>sudo command</strong> as it uses the encrypted key.</p>
<pre><code class="lang-bash">sudo ./bin/kc.sh start-dev
</code></pre>
<h2 id="heading-demo-with-http-and-https">Demo with http:// and https://</h2>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://www.youtube.com/watch?v=TgJ8KO7ys9U">https://www.youtube.com/watch?v=TgJ8KO7ys9U</a></div>
<p> </p>
<h2 id="heading-securing-secrets-with-vault"><strong>Securing Secrets with Vault</strong></h2>
<p>Hardcoding secrets is dangerous. Vault allows you to securely manage and access secrets like private keys or client secrets for Keycloak, especially in production environments.</p>
<p>First Install vault CLI</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757500372988/159da54e-a30d-4a3b-9282-0b7f16b88834.png" alt /></p>
<pre><code class="lang-bash">vault server -dev
<span class="hljs-built_in">export</span> VAULT_ADDR=<span class="hljs-string">"http://127.0.0.1:8200"</span>
<span class="hljs-built_in">export</span> VAULT_TOKEN=<span class="hljs-string">"YOUR_GENERATED_TOKEN"</span>
</code></pre>
<pre><code class="lang-bash">vault kv put secret/database password=<span class="hljs-string">"demo123"</span>
vault kv get sectret/database
</code></pre>
<p>This starts vault in dev mode <a target="_blank" href="http://127.0.0.1:8200">http://127.0.0.1:8200</a>. I<a target="_blank" href="http://127.0.0.1:8200">t Will print a root t</a>oken in the console, save it – you will need it to authenticate.</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://youtu.be/39srrWICF1Q">https://youtu.be/39srrWICF1Q</a></div>
<p> </p>
<h4 id="heading-with-vault-you-can">With Vault, you can:</h4>
<ul>
<li><p>Store <strong>TLS private keys</strong>, DB passwords, and admin credentials securely</p>
</li>
<li><p><strong>Fetch secrets at runtime</strong> via an entrypoint script or external tool</p>
</li>
<li><p>Avoid hardcoding secrets in <code>keycloak.conf</code> or Docker images</p>
</li>
<li><p>Rotate secrets <strong>without redeploying</strong> Keycloak</p>
</li>
</ul>
]]></content:encoded></item></channel></rss>