I have created a simple ASP.NET MVC4 web site to test the new OWIN Authentication middleware, I decided to start with Google OAuth2, I have had struggle quite a bit with the
After countless hours of reading on the web for answers I decided to debug the OWIN source code to find a solution to this problem, while the debugging session I came accross this gem in the AuthenticationHandler
class
if (BaseOptions.AuthenticationMode == AuthenticationMode.Active)
{
AuthenticationTicket ticket = await AuthenticateAsync();
if (ticket != null && ticket.Identity != null)
{
Helper.AddUserIdentity(ticket.Identity);
}
}
In my original Startup
class I was enabling the external sign in cookie with this method
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
This method was using a default CookieAuthenticationOptions
instance that had AuthenticationMode = AuthenticationMode.Passive
and this was preventing the class from reading the information stored in the cookie, that way on every new request the OwinContext was not loading the authenticated identity and it resulted on Request.IsAuthenticated
After I realized this all I did was to change app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
with this
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationMode = AuthenticationMode.Passive,
AuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
ExpireTimeSpan = TimeSpan.FromMinutes(30)
});
and everything worked beautifully
I had the same problem but all I needed to do was add "Google + API" to my list of APIS in the google developer console. After that, everything worked.