I was using this library: https://github.com/dylanplecki/KeycloakOwinAuthentication/tree/master
and recently we wanted to upgrade to use Owin middleware, in summary this is my code,
i noticed that sometimes it logs in correctly, and sometimes it’s stuck in an infinite loop of redirection that stops once i force refresh the page in the browser, after that it login correctly.
`
app.SetDefaultSignInAsAuthenticationType(persistentAuthType);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = persistentAuthType,
AuthenticationMode = AuthenticationMode.Active,
CookieManager = new Microsoft.Owin.Host.SystemWeb.SystemWebChunkingCookieManager(),
CookieSecure = CookieSecureOption.Always,
CookieHttpOnly = true
});
var authenticationOptions = new OpenIdConnectAuthenticationOptions
{
ClientId = WebConfigurationManager.AppSettings["ClientIdWeb"],
CookieManager = new Microsoft.Owin.Host.SystemWeb.SystemWebChunkingCookieManager(),
ClientSecret = WebConfigurationManager.AppSettings["ClientSecretWeb"],
Authority = WebConfigurationManager.AppSettings["KeyCloakUrl"] + "/auth/realms/" + WebConfigurationManager.AppSettings["Realm"],
Scope = OpenIdConnectScope.OpenIdProfile,
AuthenticationMode = AuthenticationMode.Active,
ResponseType = OpenIdConnectResponseType.Code,
SignInAsAuthenticationType = persistentAuthType,
ProtocolValidator = new OpenIdConnectProtocolValidator()
{
RequireNonce = false,
RequireStateValidation = false
},
RedirectUri = WebConfigurationManager.AppSettings["KeyCloakRedirectURI"],
RedeemCode = true,
SaveTokens = true,
UsePkce = false,
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
RoleClaimType = ClaimTypes.Role,
NameClaimType = "preferred_username"
}
};
app.UseOpenIdConnectAuthentication(
authenticationOptions
);
I am not so much familiar with the authentication flow and i tried many solutions but still the same issue.
when i try to add logs in OpenIdConnectAuthenticationNotifications, it seems to get stuck at MessageReceived.
and also i noticed that during the loop the cookies are set and removed at each redirection.
I tried using SystemWebCookieManager, but the problem was not resolved, and it was not affected by the force-refresh.
Mds is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.