2

I'm trying to get a user's date of birth and avatar/icon/photo/picture when he or she logs into my MVC5 site with a Google account.

I've read this:

Get ExtraData from MVC5 framework OAuth/OWin identity provider with external auth provider

but it doesn't make much sense ... ... am I suppose to know this?

In my Startup.Auth.cs file i have this snippet:

var gProvider = new GoogleAuthenticationProvider { OnAuthenticated = context => { var claims = context.Identity.Claims.ToList(); return Task.FromResult(0); } };
var gOptions = new GoogleAuthenticationOptions { Provider = gProvider };
app.UseGoogleAuthentication(gOptions);

The claims variable contains 5 items: (each item starts with 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/')

0: { nameidentifier: https://www.google.com/accounts/.../id?id... }
1: { givenname: Benjamin }
2: { surname: Day }
3: { name: Benjamin Day }
4: { emailaddress: ben@sillypenguin.net }

Can anyone help point out what I'm doing wrong or what I'm missing to get the extra profile data I'm looking for?

Community
  • 1
  • 1
Skipper Ben
  • 31
  • 1
  • 5

1 Answers1

4

if in short, you have to use OAuth 2.0 integration with Google instead of Open ID that enabled by default.

public class CustomGoogleProvider : GoogleOAuth2AuthenticationProvider
{
    public override Task Authenticated(GoogleOAuth2AuthenticatedContext context)
    {
        context.Identity.AddClaim(new Claim("picture", context.User.GetValue("picture").ToString()));
        context.Identity.AddClaim(new Claim("profile", context.User.GetValue("profile").ToString()));

        return base.Authenticated(context);
    }
}

and use it as

    var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
    {
        ClientId = "{{Your Client Id}}",
        ClientSecret = "{{Your Client Secret}}",
        CallbackPath = new PathString("/Account/ExternalGoogleLoginCallback"),
        Provider = new CustomGoogleProvider(),
    };

    googleOAuth2AuthenticationOptions.Scope.Add("email");

    app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);

I described the process in details here

Community
  • 1
  • 1
Mr. Pumpkin
  • 6,212
  • 6
  • 44
  • 60