如何在ASP.NET MVC中登录时获取Google+个人资料图片 [英] How to get Google+ profile picture on login in ASP.NET MVC

查看:52
本文介绍了如何在ASP.NET MVC中登录时获取Google+个人资料图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有实体框架(6.0版)应用程序的ASP.NET MVC 5。
我已经添加了简单的Google登录名,该登录名与注册时的用户一起保存了Google电子邮件。

I'm working on a ASP.NET MVC 5 with Entity Framework (version 6.0) application. I have added the simple google login, that saves the google email with the user on registration. How do I also get the profile picture of the Google+ user when they login and cast it in a view?

推荐答案

Google Plus API,当他们登录并投射到视图中时,如何获取他们的个人资料图片?开发人员版可让您从Google+获取公共数据。
,然后是详细教程,详细说明了从Google+成功获取公共数据所需执行的所有必要步骤。
Google表示Google+ API的使用限制-每个开发人员都有配额。

Google Plus API for developers allows you to fetch public data from Google+. Followed by detail tutorial of all the necessary steps one need to perform to successfully fetch public data from Google+. Google implies a limit to the usage of Google+ API - Each developer has a quota. We will see about that when we will discuss Google API console.

Google在尝试访问用户数据时使用OAuth2.0协议授权您的应用程序。

Google uses OAuth2.0 protocol to authorize your application when it tries to access user data.

它主要通过RESTful API设计使用标准的HTTP方法来获取和操作用户数据。
Google使用JSON数据格式表示API中的资源。
步骤1:通过Google API控制台生成API密钥。
步骤2:使用了GoogleOAuth2AuthenticationOptions,这意味着您需要在 https://console.developers.google.com/project 首先获取ClientId和ClientSecret。

It mostly uses standard HTTP method by means of RESTful API design to fetch and manipulate user data. Google uses JSON Data Format to represent the resources in the API. Step1: Generate an API key through Google API Console. Step2: 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.

在该链接上( https://console.developers.google.com/project ),创建一个项目,然后选择它。
然后在左侧菜单上,单击 APIs& auth。
在 API下,确保将 Google+ API设置为开。
然后单击凭据(在左侧菜单中)。
然后单击创建新的客户ID按钮。
按照说明进行操作,然后会为您提供ClientId和ClientSecret,请同时注意两者。

At that link (https://console.developers.google.com/project), create a project and then select it. Then on the left side menu, click on "APIs & auth". Under "APIs", ensure you have "Google+ API" set to "On". Then click on "Credentials" (in the left side menu). 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.

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

//获取用于个人资料图片请求的访问令牌

//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;
        }

OR

var info = await signInManager.GetExternalLoginInfoAsync();
var picture = info.ExternalPrincipal.FindFirstValue("pictureUrl");

ExternalLoginCallback方法我检查正在使用哪个登录提供程序并处理Google登录数据。
通过链接获取更多信息。
https://developers.google.com/identity/protocols/OAuth2

ExternalLoginCallback method I check for which login provider is being used and handle the data for Google login. Go through the link to get more information. https://developers.google.com/identity/protocols/OAuth2

我已经尝试过了。

这篇关于如何在ASP.NET MVC中登录时获取Google+个人资料图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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