using GoogleOAuth2AuthenticationOptions got a redirect_uri_mismatch error

随声附和 提交于 2019-12-04 08:26:16

Use only this much.

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 google callback location in the google console to point to this address.

Add RouteConfig file

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

Use this Below Code Snippet which is working 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 still error exists

Assume if your application URI is as shown below

http://localhost:2625/

Then at console.developers.google.com the URI you have registered need to be changed as show below.

Just add [signin-google] in URI at end

http://localhost:2625/signin-google

And finally save it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!