1

I'm trying to use oauth with Google in ASP.NET MVC 5.

In Google's developer console I put for the redirect uri:

www.mydomain.com/account/externallogincallback

and thought that this will do. But it didn't.

I put:

www.mydomain.com/signin-google

and it worked!

I tried to search the string "signin-google" in my project but couldn't find it anywhere.

Can someone tell me what is going on? why is that so? thanks.

span
  • 5,405
  • 9
  • 57
  • 115
dsb
  • 2,347
  • 5
  • 26
  • 43
  • If you put in google console that url how do you want to appear in your MVC project? and what do you mean with "it worked", google console let you save the url, or you can login on your website with google account? witch i doubt will work. – SilentTremor Oct 16 '15 at 09:02
  • @SilentTremor thanks for your comment. I had to put signin-google in my Google Developer Console instead of the ExternalLoginCallback from the default Account controller of MVC. It worked means that I was able to login. Much to my surprise and as I understand yours as well. I'd love someone to explain me this issue since I intend to add many more OAuth providers to my website and I would like to understand the issue fully. – dsb Oct 16 '15 at 20:54

1 Answers1

7

I am too lazy to write a properly formatted answer, I placed these comments in code for myself to remember how to resolve this issue. It is not really an issue, just something I never bothered to read properly :) But this is what you can do to make it work. There 2 options how you can do it. I have tried both and both options work just fine. I went with the first one for now, it really doesnt matter. Here are my comments in Startup.Auth.cs file.

// My notes to resolve Google Error: redirect_uri_mismatch error
// By default GoogleOAuth2AuthenticationOptions has CallbackPath defined as "/signin-google"
// https://msdn.microsoft.com/en-us/library/microsoft.owin.security.google.googleoauth2authenticationoptions(v=vs.113).aspx
// But the real path should be Controller/Action: for this application it is "/Account/ExternalLoginCallback"

// There are 2 ways to define it properly:
// 1) Add a new route in RouteConfig.cs that will map "/signin-google" into "/Account/ExternalLoginCallback":
// routes.MapRoute(name: "signin-google", url: "signin-google", defaults: new { controller = "Account", action = "ExternalLoginCallback" });
// Remember, in Google Developers Console you must have your "/signin-google" redirect URI, since that is what your app sends to Google

// 2) Completely overwrite built-in "/signin-google" path.
// Owerwrite CallbackPath right here by adding this line after ClientSecret:
// CallbackPath = new PathString("/Account/ExternalLoginCallback")
// Remember, in Google Developers Console you must have "/Account/ExternalLoginCallback" redirect URI, since now that is what your app sends to Google

app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
    ClientId = "xxxxxxxxxxxxxxxxxxxx",
    ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxx"
});
monstro
  • 6,254
  • 10
  • 65
  • 111