如何获得谷歌加的个人资料图片在C#MVC认证 [英] How to get google plus profile picture in c# MVC authentication

查看:211
本文介绍了如何获得谷歌加的个人资料图片在C#MVC认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用谷歌作为一个默认的提供者签订的C#ASP.NET MVC 5应用程序。登录功能工作正常,我可以得到用户的电子邮件和名称。
有一件事,我需要的是让用户的个人资料图片。

I'm developing a C# ASP.NET MVC 5 application that uses Google sign in as a default provider. The login functionality works ok and I can get the e-mail and name of the user. One thing that I need is to get the profile picture of the user.

我怎样才能做到这一点?

How can I achieve that?

到目前为止,我使用的是默认的MVC AUTHUseGoogleAuthentication。

So far I using the default MVC auth "UseGoogleAuthentication".

Microsoft.Owin.Security.Google.GoogleAuthenticationOptions a = new Microsoft.Owin.Security.Google.GoogleAuthenticationOptions();

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);

这是我怎么能获得的电子邮件地址。但是,我们的档案图片吗?

This is how I can get the email address. But what about the profile picture?

我是否需要使用身份验证的另一种形式?

Do I need to use another form of authentication?

推荐答案

我知道这是一个迟到的答案时,却发现你的问题解决同一个问题工作时。这里是我的解决方案。

I know this is a late answer, but found your question while working on the same problem. Here is my solution.

使用 GoogleAuthenticationOptions 我用 GoogleOAuth2AuthenticationOptions 这意味着你需要建立一个项目,<一个相反的href=\"https://console.developers.google.com/project\">https://console.developers.google.com/project首先要获得客户端Id ClientSecret

Instead of using GoogleAuthenticationOptions I used GoogleOAuth2AuthenticationOptions which means you'll need to set up a project at https://console.developers.google.com/project first to get a ClientId and ClientSecret.


  1. 目前该链接(<一个href=\"https://console.developers.google.com/project\">https://console.developers.google.com/project),创建一个项目,然后选择它。

  1. At that link (https://console.developers.google.com/project), create a project and then select it.

然后在左侧菜单中,点击API和放大器; AUTH。

Then on the left side menu, click on "APIs & auth".

在的API,确保你有Google+的API设置为开。

Under "APIs", ensure you have "Google+ API" set to "On".

然后点击证书(在左侧菜单)。

Then click on "Credentials" (in the left side menu).

然后点击按钮创建新客户端ID。按照指示,然后你将提供一个客户端Id ClientSecret ,取两者的注释。

Then click on the button "Create new Client ID". Follow the instructions and you will then be provided with a ClientId and ClientSecret, take note of both.

现在你拥有了这些,在 GoogleOAuth2AuthenticationOptions code是这样的:

Now you have those, the GoogleOAuth2AuthenticationOptions code looks like this:

var googleOptions = new GoogleOAuth2AuthenticationOptions()
{
    ClientId = [INSERT CLIENT ID HERE],
    ClientSecret = [INSERT CLIENT SECRET HERE],
    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);

请注意,这也增加了访问令牌作为索赔,所以我们可以用它来获取个人资料图片。下位可能会根据您如何您的项目设置有所不同,但对我来说,它是在的AccountController

在我的 ExternalLoginCallback 方法我检查哪些登录正在使用和处理的谷歌登录数据提供者。在这一节中,我检索资料图片网址,并将其与以下code存储在一个变量:

In my ExternalLoginCallback method I check for which login provider is being used and handle the data for Google login. In this section I retrieve the profile image url and store it in a variable with the following code:

//get access token to use in profile image request
var accessToken = loginInfo.ExternalIdentity.Claims.Where(c => c.Type.Equals("urn:google:accesstoken")).Select(c => c.Value).FirstOrDefault();
Uri apiRequestUri = new Uri("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + accessToken);
//request profile image
using (var webClient = new System.Net.WebClient())
{
    var json = webClient.DownloadString(apiRequestUri);
    dynamic result = JsonConvert.DeserializeObject(json);
    userPicture = result.picture;
}

本使用访问令牌请求从谷歌的用户信息。然后检索从JSON数据返回图像的URL。然后,您可以将网址保存到数据库中为您的项目最合适的方式。

This uses the access token to request the user info from google. It then retrieves the image url from the json data return. You can then save the url to the database in the most appropriate way for your project.

希望有所帮助别人。

这篇关于如何获得谷歌加的个人资料图片在C#MVC认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆