Claiming the users profile picture and name, when registering with Google account in ASP.NET MVC 5

asp.net asp.net-mvc c# entity-framework-6 oauth

Popular Answer

According to code example provided by Google, you must first haveOnAuthenticated callback to record the picture URL during login time:

// After OAuth authentication completes successfully,
// read user's profile image URL from the profile
// response data and add it to the current user identity
OnAuthenticated = context =>
{
    var profileUrl = context.User["image"]["url"].ToString();
    context.Identity.AddClaim(new Claim(ClaimTypes.Uri, profileUrl));
    return Task.FromResult(0);
}

The User class then allows you to access both the name and picture URL:

public class User : ClaimsPrincipal
{
    public User(IPrincipal principal) : base(principal as ClaimsPrincipal) { }

    public string Name => this.Identity.Name;
    public string UserId => this.FindFirst(ClaimTypes.NameIdentifier).Value;
    public string ProfileImage => this.FindFirst(ClaimTypes.Uri).Value;
}
0
4/29/2017 4:36:48 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow