Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OWIN - Authentication.SignOut() doesn't seem to remove the cookie

I'm having some issues with OWIN Cookie authentication. I have a .Net site that has some MVC pages which uses cookie authentication and WebAPI resources protected by a bearer token.

When I log out, I delete the access token on the client, so subsequent API requests will not have the token in the header and will thus fail the authentication. This part is fine.

In the same manner, I would also like the log out to delete the cookie used by the MVC pages. I did the following on the server:

    [Route("Logout")]     public IHttpActionResult Logout()     {         var ctx = Request.GetOwinContext();         var authenticationManager = ctx.Authentication;         authenticationManager.SignOut();         return Ok();     } 

However, after the calling Logout, I can still visit the protected MVC page even though the cookie would have supposedly been deleted by the Logout call.

It seems so simple, so I might have missed something.

Thanks,

like image 998
jaeyow Avatar asked Mar 11 '15 23:03

jaeyow


People also ask

How do I log out a user using OWIN Cookie authentication middleware?

Logging a user out is quite simple — there’s an API on the OWIN authentication manager called SignOut, which removes the cookie: I hope this helps to understand the new OWIN cookie authentication middleware in .NET 4.5.1 and Visual Studio 2013.

Why the SIGNOUT methods never work with OWIN?

After all these years of development with Forms, Cookies, etc., and now OWIN, can't understand why the SignOut methods never work and the only thing that works is to expire the cookies by ourselves... What worked for me was the bellow code. Please notice that AuthenticationManager.SignOut that is in the samples of Microsoft is there doing nothing.

What is the difference between forms authentication and OWIN Cookie authentication?

One improvement the OWIN cookie authentication middleware has over the previous Forms authentication is that it is claims-aware. Another function of Forms authentication was that when the application issued a 401 unauthorized HTTP status code, Forms authentication would convert the response into a 302 redirect to the application’s login page.

Does logout delete the cookie used by the MVC pages?

This part is fine. In the same manner, I would also like the log out to delete the cookie used by the MVC pages. I did the following on the server: However, after the calling Logout, I can still visit the protected MVC page even though the cookie would have supposedly been deleted by the Logout call.


1 Answers

I had a similar problem for the past few days. Instead of

Request.GetOwinContext().Authentication.authenticationManager.SignOut(); 

Use ONE(and only one) of these:

Request.GetOwinContext().Authentication.SignOut();  Request.GetOwinContext().Authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);  HttpContext.Current.GetOwinContext().Authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie); 

This article explains why your cookies don't get deleted: https://dzone.com/articles/catching-systemwebowin-cookie

I know my answer isn't the most research-based, but to tell you the truth, I just couldn't find WHY my provided code examples work for me. I just know that System.Web messes up Owins cookies if you do SignOut() another way.

like image 142
Stralos Avatar answered Sep 20 '22 15:09

Stralos