好的...我目前正在使用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函數中檢索一次配置文件映像?
何你知道API密鑰嗎?因爲當你註冊一個新的應用程序到谷歌他們不提供你的API密鑰? – CalinCosmin
@CalinCosmin我將它添加到答案。 – Norman
謝謝。這絕對幫助我 – CalinCosmin