I have succesfuly integrated Keycloak into my ASP.NET MVC application like so
const string persistentAuthType = "keycloak_auth";
app.SetDefaultSignInAsAuthenticationType(persistentAuthType);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = persistentAuthType,
});
// Configure Auth0 authentication
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
AuthenticationType = "keycloak_auth",
ClientId = clientId,
Authority = authorityserver,
RedirectUri = "http://localhost:13636/home",
PostLogoutRedirectUri = "http://localhost:13636",
ClientSecret = clientSecret,
RequireHttpsMetadata = false,
ResponseType = OpenIdConnectResponseType.Code,
Scope = "openid profile email",
});
The test is ok, the Keycloak login page is displayed and i can login to it and it redirects to my /home controller correctly, however in the Home controller, when checking the User.Identity.IsAuthenticated it always return to false;
public class HomeController : BaseController
{
public ActionResult Index()
{
if (!User.Identity.IsAuthenticated) // <-- always false
{
HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties
{
RedirectUri = Url.Action("Index", "Home")
}, "keycloak_auth");
return new HttpUnauthorizedResult();
}
ViewBag.Title = "Home";
return View();
}
Why is this? Am I missing something? since it is false, it always calls it back
/home and landing on the same conditions inside the IsAuthenticated (false) thus “crashing” since it overruns stack.
Anyone can help?