A set of technologies in .NET for building web applications and web services. Miscellaneous topics that do not fit into specific categories.
Hi @Hamed Vaziri ,
Sorry for bother you again. I know you have found a solution that is currently working
Since we have different approaches on this topic, here's a clear summary of what I've learned—and what's worked for myself and yours. This might help others who encounter the same solution.
1. Authorization Code Flow (response_type=code)
Solution:
To resolve issues with the Authorization Code Flow, ensure your Keycloak client is set to "Standard Flow" (authorization code), check that your OWIN middleware is configured with ResponseType = "code" and RedeemCode = true, set handling authentication type correctly and confirm your redirect URI matches in both Keycloak and your app. If you see a 404 on /signin-oidc, double-check your OWIN pipeline and middleware order.
What it does:
This is the most secure and modern way to use OpenID Connect. After login, Keycloak sends your app an authorization code, which your backend exchanges for tokens.
Should it work with OWIN?
- In my experience, it does work with ASP.NET MVC and OWIN, as long as your middleware is set up correctly and your Keycloak client uses the correct redirect URI and response type.
- The OWIN middleware should handle the
/signin-oidccallback and redeem the code. - Make sure you have
RedeemCode = true, and your Keycloak client is set to "Standard Flow" (authorization code) enabled.
Why it might not work (for some):
- The official OWIN OpenID Connect middleware has some quirks and was originally designed for Azure AD. Certain older versions or misconfigurations may not redeem the code properly.
- If you see a 404 on
/signin-oidc, double-check your OWIN pipeline, Keycloak redirect URI, and middleware setup. - If your team lead says "code" doesn't work, it's possible your environment or package versions differ.
2. Implicit Flow (response_type=id_token)
Solution:
If you run into trouble with Authorization Code Flow, switch to Implicit Flow by setting ResponseType = "id_token" in your middleware and enabling "Implicit Flow" in your Keycloak client settings. This lets OWIN handle the authentication without needing to exchange an authorization code for tokens.
What it does:
With Implicit Flow, Keycloak sends the id_token directly in the redirect—no backend code exchange needed.
Should it work with OWIN?
- Yes! The OWIN middleware natively handles the
id_tokenfrom the callback. - You must enable "Implicit Flow" for your Keycloak client.
- Use
ResponseType = "id_token"(or"id_token token"for access token too).
To compare the 2 approaches:
- Security: Authorization Code Flow is recommended for production apps.
- Compatibility: If your current setup or team prefers Implicit Flow due to middleware limitations, it’s a workable solution.
- What’s best? If you can get Code Flow working, stick with it. If not, Implicit Flow is a valid fallback for many .NET Framework MVC apps.
I hope that my answer is helpful. If possible, please consider letting me know if I am missing anything on this problem.