Google Authentication using OWIN Oauth in MVC5 not hitting ExternalLoginCallback function

前端 未结 4 1415
不思量自难忘°
不思量自难忘° 2020-12-09 03:36

I am currently upgrading my login process for Google to use OAuth before they depricate their OpenID login method.

The steps I have Identified so far is that I have

相关标签:
4条回答
  • 2020-12-09 03:45

    Try only this

    var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
            {
                ClientId = "MYCLIENTID",
                ClientSecret = "MYSECRET",
            };
    app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);
    

    This worked for me

    0 讨论(0)
  • 2020-12-09 03:48

    I'm using the default ASP.NET MVC 5 template with Identity Authentication for simplicity, but hopefully this can be modified for different use cases.

    StartupAuth.cs

    Do not customize the redirect path. It gets replaced by /signin-google anyways and my attempts at getting around that caused "silent" (not in the debugger) Internal Server 500 errors.

    app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
    {
        ClientId = "whatevs.apps.googleusercontent.com",
        ClientSecret = "whatevs_secrut",
        Provider = new GoogleOAuth2AuthenticationProvider()
    });
    

    Make sure to add http://whatever.com/signin-google to https://console.developers.google.com/ in your APIs & auth > Credentials > Redirect URIs section.

    RouteConfig.cs

    Add a route to a permanent redirect controller action to your routes. Permanent redirects are the only thing that will suffice here. It is not enough to simply direct directly to the Callback URL.

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute(
            name: "Google API Sign-in",
            url: "signin-google",
            defaults: new { controller = "Account", action = "ExternalLoginCallbackRedirect" }
        );
    
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
    

    AccountController.cs

    Permanent redirect to the built-in callback method and you should be fine.

    [AllowAnonymous]
    public ActionResult ExternalLoginCallbackRedirect(string returnUrl)
    {
        return RedirectPermanent("/Account/ExternalLoginCallback");
    }
    

    A template project has been posted on GitHub for reference: https://github.com/Pritchard/Test-AspNetGoogleOAuth2Authentication

    0 讨论(0)
  • 2020-12-09 03:56

    Make sure you have also enabled the Google+ API in your developer console. It is an additional step after you have your client and secret

    0 讨论(0)
  • 2020-12-09 03:56

    As @Ronen said in the comments, this link should solve the issues with Google OAuth in MVC5:

    http://blogs.msdn.com/b/webdev/archive/2014/07/02/changes-to-google-oauth-2-0-and-updates-in-google-middleware-for-3-0-0-rc-release.aspx

    Also update the OWIN packages from NuGet. This is how my code looks and is working great:

           var googleOptions = new GoogleOAuth2AuthenticationOptions ()
           {
               ClientId = "xxxxxxxxxx",
               ClientSecret = "xxxxxxxxxx",
               CallbackPath = new PathString("/signin-google")
           };
           googleOptions.Scope.Add("email");
    
           app.UseGoogleAuthentication(googleOptions);
    
    0 讨论(0)
提交回复
热议问题