Using GoogleOAuth2AuthenticationOptions received redirect_uri_mismatch error

I am trying to implement Google authentication in the MVC 5 web application. Authentication works fine, but I would like to get profile and image information.

To do this, I added a GoogleOAuth2AuthenticationOptions object to indicate additional claims:

var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions { ClientId = "xxxxxxxxxxxxxxxx", ClientSecret = "xxxxxxxxxxxxxxxxx", CallbackPath = new PathString("/Account/LoginCallback"), Provider = new GoogleOAuth2AuthenticationProvider() { OnAuthenticated = async context => { context.Identity.AddClaim(new Claim("picture", context.User.GetValue("picture").ToString())); context.Identity.AddClaim(new Claim("profile", context.User.GetValue("profile").ToString())); } } }; app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions); 

But this causes the wrong url:

http://admin.localhost.com/Account/LoginCallback&state=Gs-otJmI79bgWA3_qJzDcGziWnkRCOf7JRoCUDCIz0jv4IIvDdoZlZzVSq2kZxfaPFDmv9hbZGp5q1Aq8mpLPguKnCF31twHj8NQCMv_NrgZzvKwaelmZr_HwY_bdj8h1ICFrkGTKLJ1saEYDbFJ2CJxvDkyBL2iygQmTXQTs-aUiL4yWe5_7dZQOjP_mDUSW-GXns3wr7Okwkoj8VEITJTUz9nAbrBd_N_7puTMlHU&client_id=xxxxxxxxxxxxxxxx

Not '?' before parameters calling redirect_uri_mismatch.

However, when I use simply:

 app.UseGoogleAuthentication( clientId : "xxxxxxxxxxxxxxxxxxx", clientSecret : "xxxxxxxxxxxxxxxxx"); 

Works.

Any idea?

+5
source share
2 answers

Use just that.

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

This method seems to automatically use the signin-google request in the address. To fix this change in the Google callback location in the Google console, point to this address.

Add RouteConfig File

  routes.MapRoute( name: "signin-google", url: "signin-google", defaults: new { controller = "Account", action = "LoginCallback" } ); 
+5
source

Use this code snippet below that works fine, just replace ClientID and ClientSecret will work for you.

  var googleOptions = new GoogleOAuth2AuthenticationOptions() { ClientId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", ClientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", Provider = new GoogleOAuth2AuthenticationProvider() { OnAuthenticated = (context) => { context.Identity.AddClaim(new Claim("urn:google:name", context.Identity.FindFirstValue(ClaimTypes.Name))); context.Identity.AddClaim(new Claim("urn:google:email", context.Identity.FindFirstValue(ClaimTypes.Email))); //This following line is need to retrieve the profile image context.Identity.AddClaim(new System.Security.Claims.Claim("urn:google:accesstoken", context.AccessToken, ClaimValueTypes.String, "Google")); return Task.FromResult(0); } } }; app.UseGoogleAuthentication(googleOptions); 

if error still exists

Assume your application URI as below

http: // localhost: 2625 /

Then, on console.developers.google.com, the URI that you registered must be changed, as shown below.

Just add [ signin-google ] to the URI at the end

http: // localhost: 2625 / signin-google

And finally save it.

AX3hk.jpg

+1
source

Source: https://habr.com/ru/post/1203165/


All Articles