Share via

Owin security could not handle callback "signin-oidc", Error 404 On Asp.net Mvc!

Hamed Vaziri 156 Reputation points
2025-09-25T22:26:01.2233333+00:00

Hi everyone

I have a simple asp.net mvc app with .net framework 4.8.

I want to use keycloak as security management and my app connect to it via openid connect protocol.

To do this, I've created an startup.cs class in my project as follow :

[assembly: OwinStartup(typeof(Mvc48Keycloak.Startup))]
namespace Mvc48Keycloak
{
	public class Startup
	{
		public void Configuration(IAppBuilder app)
		{
			ConfigureAuth(app);
		}
		
		public void ConfigureAuth(IAppBuilder app)
		{
			var clientId = ConfigurationManager.AppSettings["Keycloak:ClientId"];
			var clientSecret = ConfigurationManager.AppSettings["Keycloak:ClientSecret"];
			var redirectUri = ConfigurationManager.AppSettings["Keycloak:RedirectUri"];
			
			app.UseCookieAuthentication(new CookieAuthenticationOptions
			{
				AuthenticationType = "Cookies"
			});
			
			app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
			{
				AuthenticationType = "oidc",
				Authority = "http://localhost:8080/realms/aspnet", // my keycloak server
				ClientId = clientId,
				ClientSecret = clientSecret,
				RedirectUri = redirectUri,
				ResponseType = "code",
				Scope = "openid profile email offline_access",
				TokenValidationParameters = new TokenValidationParameters
				{
					ValidateIssuer = true,
					ValidIssuer = "http://localhost:8080/realms/aspnet",
					ValidateAudience = true,
					ValidAudience = clientId,
				},
				RequireHttpsMetadata = false,
				SignInAsAuthenticationType = "Cookies",
				CallbackPath = new PathString("/signin-oidc"),
			}
		}
	}
}

And here is my web.config settings :

<appSettings>
	<!-- Keycloak Configuration -->
	<add key="Keycloak:ClientId" value="myClientID" />
	<add key="Keycloak:ClientSecret" value="myClientSecret" />
	<add key="Keycloak:RedirectUri" value="https://localhost:44300/signin-oidc" />
	...
</appSettings>

To test authentication process, i've simply placed [Authorize] attribute on Contact action inside HomeController :

[Authorize]
public ActionResult Contact()
{
    ViewBag.Message = "Your contact page.";
    return View();
}

At runTime, i've redirect to keycloak login form, but after that, when redirect to my app, facing this error 404 :

The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /signin-oidc

Apparently owin should handle the callbacks url but i don't know it could not!

Can anybody help me where is the problem and how to solve it?

Thanks in advance

Developer technologies | ASP.NET | Other

Answer accepted by question author

  1. Danny Nguyen (WICLOUD CORPORATION) 6,785 Reputation points Microsoft External Staff Moderator
    2025-10-06T09:10:52.7133333+00:00

    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-oidc callback 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_token from 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.

    2 people found this answer helpful.
    0 comments No comments

Answer recommended by moderator

  1. Hamed Vaziri 156 Reputation points
    2025-09-26T11:48:06.2466667+00:00

    Hi again!
    Thanks for your helps..

    I've solved my problem, here is the solution :
    Based on this thread, we have to set ResponseTypes to "id_token", but In addition to that, we have to enable "Implecit flow" in keycloak server to receive id_token without authorization code!

    That's it!
    best regards ..


1 additional answer

Sort by: Most helpful
  1. Sheeraz Ali 170 Reputation points
    2025-09-26T09:04:21.7933333+00:00
    1. Close the missing bracket in UseOpenIdConnectAuthentication → should end with });.
    2. Remove RedirectUri = redirectUri, from your config (keep only CallbackPath = new PathString("/signin-oidc")).
    3. In Keycloak, add https://localhost:44300/signin-oidc under Valid Redirect URIs.
    4. Ensure app.UseCookieAuthentication comes before app.UseOpenIdConnectAuthentication.

    That will stop MVC from giving 404 on /signin-oidc.

    Here’s the corrected Startup.cs auth section you can drop in:

    public void ConfigureAuth(IAppBuilder app)
    {
        var clientId = ConfigurationManager.AppSettings["Keycloak:ClientId"];
        var clientSecret = ConfigurationManager.AppSettings["Keycloak:ClientSecret"];
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = "Cookies"
        });
        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
        {
            AuthenticationType = "oidc",
            Authority = "http://localhost:8080/realms/aspnet",
            ClientId = clientId,
            ClientSecret = clientSecret,
            ResponseType = "code",
            Scope = "openid profile email offline_access",
            TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = true,
                ValidIssuer = "http://localhost:8080/realms/aspnet",
                ValidateAudience = true,
                ValidAudience = clientId,
            },
            RequireHttpsMetadata = false,
            SignInAsAuthenticationType = "Cookies",
            CallbackPath = new PathString("/signin-oidc")
        });
    }
    
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.