ASP.NET Identity OWIN Middleware Google OAuth2 AuthenticationManager SignIn not working

前端 未结 2 833
不思量自难忘°
不思量自难忘° 2021-01-20 08:58

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

相关标签:
2条回答
  • 2021-01-20 09:04

    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

    0 讨论(0)
  • 2021-01-20 09:17

    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.

    0 讨论(0)
提交回复
热议问题