2017-08-24 56 views
1

好的...我目前正在使用ASP.Net Core 1.1.2和ASP.NET Core Identity 1.1.2。如何使用ASP.Net Core Identity從登錄用戶檢索Google個人資料圖片?

Startup.cs最重要的部分是這樣的:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     //... 
     app.UseGoogleAuthentication(new GoogleOptions 
     { 
      AuthenticationScheme = "Google", 
      SignInScheme = "Identity.External", // this is the name of the cookie middleware registered by UseIdentity() 
      ClientId = Configuration["ExternalLoginProviders:Google:ClientId"], 
      ClientSecret = Configuration["ExternalLoginProviders:Google:ClientSecret"] 
     }); 
    } 

GoogleOptions自帶Microsoft.AspNetCore.Authentication.Google NuGet包。

AccountController.cs回調函數如下:

[HttpGet] 
    [AllowAnonymous] 
    public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null) 
    { 
     //... SignInManager<User> _signInManager; declared before 
     ExternalLoginInfo info = await _signInManager.GetExternalLoginInfoAsync(); 
     SignInResult signInResult = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false); 
     string email = info.Principal.FindFirstValue(ClaimTypes.Email); 
     string firstName = info.Principal.FindFirstValue(ClaimTypes.GivenName); 
     string lastName = info.Principal.FindFirstValue(ClaimTypes.Surname); 
     // 
    } 

所以,一切工作正常,直到這一點。在這裏,我卡住了。我讀了很多關於accesstokens和聲稱pictureUrl等的文章。但校長並不包含任何這些內容。

所以問題是:如何在ExternalLoginCallback函數中檢索一次配置文件映像?

回答

1

我發現沒有辦法從索賠中獲取圖片網址。最後,我找到了一個使用nameidentifier的解決方案,它附帶了聲明。

string googleApiKey = _configurationRoot["ExternalLoginProviders:Google:ApiKey"]; 
ExternalLoginInfo info = await _signInManager.GetExternalLoginInfoAsync(); 
string nameIdentifier = info.Principal.FindFirstValue(ClaimTypes.NameIdentifier); 
string jsonUrl = $"https://www.googleapis.com/plus/v1/people/{nameIdentifier}?fields=image&key={googleApiKey}"; 
using (HttpClient httpClient = new HttpClient()) 
{ 
    string s = await httpClient.GetStringAsync(jsonUrl); 
    dynamic deserializeObject = JsonConvert.DeserializeObject(s); 
    string thumbnailUrl = (string)deserializeObject.image.url; 
    byte[] thumbnail = await httpClient.GetByteArrayAsync(thumbnailUrl); 
} 

您只需要一個Google API密鑰。

要創建API密鑰:

  1. 轉到谷歌API控制檯。
  2. 從項目下拉列表中選擇一個項目或創建一個新項目。
  3. 啓用Google+ API服務:

    a。在Google API列表中,搜索Google+ API服務。

    b。從結果列表中選擇Google+ API。

    c。按下啓用API按鈕。

    當過程完成時,Google+ API將出現在啓用的API列表中。要訪問 ,請在左邊欄菜單中選擇API &服務,然後選擇 啓用的API選項卡。

  4. 在側邊欄「APIs &服務」下,選擇憑證。

  5. 在憑據選項卡中,選擇新憑據下拉列表,然後選擇API密鑰。
  6. 從創建新的密鑰彈出窗口中,爲您的項目選擇適當類型的密鑰:服務器密鑰,瀏覽器密鑰,Android密鑰或iOS密鑰。
  7. 輸入密鑰名稱,根據指示填寫其他字段,然後選擇創建。

https://developers.google.com/+/web/api/rest/oauth

+0

何你知道API密鑰嗎?因爲當你註冊一個新的應用程序到谷歌他們不提供你的API密鑰? – CalinCosmin

+1

@CalinCosmin我將它添加到答案。 – Norman

+0

謝謝。這絕對幫助我 – CalinCosmin

0

您可以在GoogleOptions設置中提供您想要請求的其他範圍。您正在尋找的範圍是profile範圍,光柵訪問名字,姓氏和輪廓通過附加的權利要求的圖片:https://developers.google.com/identity/protocols/OpenIDConnect#discovery

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     //... 
     app.UseGoogleAuthentication(new GoogleOptions 
     { 
      AuthenticationScheme = "Google", 
      SignInScheme = "Identity.External", // this is the name of the cookie middleware registered by UseIdentity() 
      ClientId = Configuration["ExternalLoginProviders:Google:ClientId"], 
      ClientSecret = Configuration["ExternalLoginProviders:Google:ClientSecret"]}); 
      Scopes = { "profile" }; 
    } 

然後你可以檢索到控制器的畫面要求認證成功後:

[HttpGet] 
    [AllowAnonymous] 
    public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null) 
    { 
     //... SignInManager<User> _signInManager; declared before 
     ExternalLoginInfo info = await _signInManager.GetExternalLoginInfoAsync(); 
     SignInResult signInResult = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false); 
     string email = info.Principal.FindFirstValue(ClaimTypes.Email); 
     string firstName = info.Principal.FindFirstValue(ClaimTypes.GivenName); 
     string lastName = info.Principal.FindFirstValue(ClaimTypes.Surname); 

     // profile claims 
     string picture = info.Principal.FindFirstValue("picture"); 
     string firstName = info.Principal.FindFirstValue("given_name"); 
     string lastName = info.Principal.FindFirstValue("family_name"); 
    } 
+1

提供名字和姓氏。但是,沒有圖片或圖片... – Norman

相關問題