Search code examples
asp.netasp.net-mvcowinkatanagoogle-signin

AuthenticationProperties.RedirectUri is not passed to Google in Challenge()


Within my web application I have registered Google as a single sign-on provider:

app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions {
    ClientId = "8765.......apps.googleusercontent.com",
    ClientSecret = "Secret"
})

My app doesn't allow users to sign-up/register (instead their accounts are created by an administrator, but they can later link their account up with Google).

In my "Sign in with Google" controller, I am trying to issue a Challenge() to redirect to Google. This might not be thecorrect approach:

string redirectUri = "http://localhost:55262/SSO/Google/ProcessToken"; // actually created in code, but shown as string for clarity
AuthenticationProperties properties = new AuthenticationProperties();
properties.RedirectUri = Server.UrlEncode(redirectUri);
Context.GetOwinContext().Authentication.Challenge(properties, "Google");

This correctly sends the user to Google, but Google then presents Error: redirect_uri_mismatch, saying that:

The redirect URI in the request: http://localhost:55262/signin-google did not match a registered redirect URI.

I've seen this error before when the return URI collection in the Google control panel does not contain the redirect_uri specified.

If I debug in VS2015, I can see the redirect_uri property being set correctly in the AuthenticationProperties, but it seems that OWIN/Katana is not passing it to Google. Instead, when I hit Google, the return_uri is the default one used by OWIN/Katana. The one I set is being ignored.

The Google request details seem to confirm this:

scope=openid profile email
response_type=code
redirect_uri=http://localhost:55262/signin-google

What am I doing wrong here please? Should I not be using Challenge() to allow users to link up their local application account with Google?


Solution

  • Note that the OWIN's Open Authentication have predefined methods. In another words, in localhost:port/signin-google, the OWIN awaits for calling the signin-google by the external authentication service (Although you can't find its implementation inside the project). The signin-google is a valid and working path and I prefoundly exhort you not to change it (due to avoid writing a new implementation as a controller action).

    I had similar trouble, After spending many weary days, finally, I found out the problem comes from the original user's URL which is effective on the sent redirect_uri by the OWIN. Clearly:

    • If you type www.site.com → redirect_uri equals to www.site.com/signin-google
    • If you type site.com → redirect_uri equals to site.com/signin-google

    And Google will return redirect_uri_mismatch Error for one of the above cases based on entered redirect URLs in Console. I think your problem comes from this reality too and the solution is setting any possible URLs in console.