0

I'm using trying to use Google authentication in an ASP.NET MVC application. For testing purposes I'm using the template app generated by VS2013 Update 4

In Google settings the return URLs are properly set and Google+ API is turned on. The app works fine when I publish it to an azure website. I can login using Google accounts without any problems.

However I'd like to deploy it on premises but here we have a reverse proxy setup which works like this:

  • the server sees itself as server01.mysite.com but this is an internal name
  • outside world sees it as www.mysite.com (certain paths are reverese proxied to the server01.mysite.com

Essentially www.mysite.com/myapp is reverse proxied to server01.mysite.com/myapp

With this setup I can't seem to use Google authentication. GetExternalLoginInfoAsync returns null and the app redirects itself to the login page.

By default the system generates a redirectUri using the private hostname. I tried changing it to the public address but this does not solve the problem. Below is what I did at startup.auth.cs

    app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
    {
        ClientId = "...",
        ClientSecret = "...",

        Provider = new GoogleOAuth2AuthenticationProvider
        {
            OnApplyRedirect = context =>
            {
                var redirectUri = context.RedirectUri.Replace("server01", "www");
                context.Response.Redirect(redirectUri);
            },
        }
    });

Is there anyway I can make Google authentication work in a setup like this?

Thanks

cellik
  • 2,116
  • 2
  • 19
  • 29

1 Answers1

0

To achieve this one has to tell the app to use the outside URL earlier so that the relevant hashes are built taking that into account. So instead of changing the redirect URI at the OnApplyRedirect call this before UseGoogleAuthentication:

      app.Use((context, next) =>
                    {
                        context.Request.Host = new HostString(
                                context.Request.Host.Value.Replace("server01", "www"));

                        return next();
                    }
                    );

and remove the Provider=... from UseGoogleAuthentication

 app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
    {
        ClientId = "...",
        ClientSecret = "..."
    });
cellik
  • 2,116
  • 2
  • 19
  • 29