dylanplecki / KeycloakOwinAuthentication

Keycloak Authentication Middleware for the C# OWIN Pipeline
http://keycloak.jboss.org
MIT License
56 stars 130 forks source link

Logout Error - Request.GetOwinContext().Authentication.SignOut() #29

Closed ntheile closed 8 years ago

ntheile commented 8 years ago

When trying to log out via Request.GetOwinContext().Authentication.SignOut(), I get the following error:

Value cannot be null. Parameter name: uriString [ArgumentNullException: Value cannot be null. Parameter name: uriString] System.Uri..ctor(String uriString) +6766781 Owin.Security.Keycloak.Middleware.d10.MoveNext() +120 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26 Owin.Security.Keycloak.Middleware.d2.MoveNext() +243 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 ...

A quick workaround is to delete the aspnet cookie and hit the OIDC logout url:

if (Request.Cookies[".AspNet.kcook"] != null) { 
        HttpCookie myCookie = new HttpCookie(".AspNet.kcook");
        myCookie.Expires = DateTime.Now.AddDays(-1d);
        Response.Cookies.Add(myCookie);
}
Response.Redirect("https://idmendpoint.com/auth/realms/MyRealm/protocol/openid-connect/logout?redirect_uri=https%3A%2F%2Flocalhost%3A44303");
greibach commented 8 years ago

Hi, I had the same issue. I modified LogoutRedirectAsync() in KeycloakAuthenticationHandler file with this:

Response.Redirect( (await KeycloakIdentity.GenerateLogoutUriAsync(Options, Request.Uri, new Uri(Options.PostLogoutRedirectUrl))) .ToString());

instead of properties.RedirectUri

dylanplecki commented 8 years ago

Fixed in version 2.1.5 using the proposed fix detailed above.

ntheile commented 8 years ago

thx @dylanplecki and @greibach !!!!