I'm trying to implement a Google authentication in a MVC 5 web app. Authentication is working fine, but I would retrieve profile and picture informations.
To do this, I added a GoogleOAuth2AuthenticationOptions object to specify 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 it causes that a wrong URL is generated :
http://admin.localhost.com/Account/LoginCallback&state=Gs-otJmI79bgWA3_qJzDcGziWnkRCOf7JRoCUDCIz0jv4IIvDdoZlZzVSq2kZxfaPFDmv9hbZGp5q1Aq8mpLPguKnCF31twHj8NQCMv_NrgZzvKwaelmZr_HwY_bdj8h1ICFrkGTKLJ1saEYDbFJ2CJxvDkyBL2iygQmTXQTs-aUiL4yWe5_7dZQOjP_mDUSW-GXns3wr7Okwkoj8VEITJTUz9nAbrBd_N_7puTMlHU&client_id=xxxxxxxxxxxxxxxx
There is no '?' before parameters, that's cause a redirect_uri_mismatch.
However, when I use simply :
app.UseGoogleAuthentication(
clientId : "xxxxxxxxxxxxxxxxxxx",
clientSecret : "xxxxxxxxxxxxxxxxx");
It's working.
Any idea ?
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" } );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With