How do i retrieve the email address when using google auth in mvc 5?

蹲街弑〆低调 提交于 2019-12-18 04:24:09

问题


I have enabled Google Auth only within my asp.net mvc 5 app. I see that when I am redirected to googles auth screen I am asking for permission to view the users name and email address. I then return from Google, log in, and name my new user.

I have obviously asked for permission to view the email address, but by default this is not stored. How would I store this in the users table?

I have tried editing the options in startup.auth, but there aren't any pertaining to the email. When doing this via oAuth you manually ask for it. I just don't know where exactly I should be asking for the email address...

Also how would I go about asking for their google account picture?


回答1:


You can retrieve it from ClaimIdentity as an Email Claim

Check this example

var email = externalIdentity.FindFirstValue(ClaimTypes.Email);




回答2:


Full code in aspnet mvc5

var googleOption=new GoogleAuthenticationOptions()
{
    Provider = new GoogleAuthenticationProvider()
    {
        OnAuthenticated =  (context) =>
        {
            var rawUserObjectFromFacebookAsJson = context.Identity;
            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)));
            return Task.FromResult(0);
        }
     }
};

app.UseGoogleAuthentication(googleOption);


来源:https://stackoverflow.com/questions/19369129/how-do-i-retrieve-the-email-address-when-using-google-auth-in-mvc-5

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