Nor*_*man 3 c# google-api google-oauth asp.net-identity asp.net-core
好的......我目前正在使用ASP.Net Core 1.1.2和ASP.NET Core Identity 1.1.2.
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"]
});
}
Run Code Online (Sandbox Code Playgroud)
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);
//
}
Run Code Online (Sandbox Code Playgroud)
所以,一切正常,直到这一点.在这里,我被困住了.我读了很多关于accessstokens和声称的文章,名为pictureUrl等.但是校长不包含任何这些.
所以问题是:如何在ExternalLoginCallback函数中检索配置文件图像一次?
我在ASP.NET Core 2.0上遇到了同样的问题.有一个更好的方法来从OnCreatingTicket你的事件中检索图片startup.cs.在您的情况下,您必须将特定声明"图片"添加到身份.
public void ConfigureServices(IServiceCollection services)
{
services
.AddAuthentication()
.AddCookie()
.AddGoogle(options =>
{
options.ClientId = Configuration["Google.LoginProvider.ClientId"];
options.ClientSecret = Configuration["Google.LoginProvider.ClientKey"];
options.Scope.Add("profile");
options.Events.OnCreatingTicket = (context) =>
{
context.Identity.AddClaim(new Claim("image", context.User.GetValue("image").SelectToken("url").ToString()));
return Task.CompletedTask;
};
});
}
Run Code Online (Sandbox Code Playgroud)
然后在您的AccountController中,您可以从外部登录信息方法中选择图像.
var info = await _signInManager.GetExternalLoginInfoAsync();
var picture = info.Principal.FindFirstValue("image");
Run Code Online (Sandbox Code Playgroud)
对于.net core 3.0+,微软使用System.Text.Json来处理google返回的对象,所以我们需要使用这个新API的GetProperty方法来获取图片。
https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/
services.AddAuthentication()
.AddGoogle(options =>
{
IConfigurationSection googleAuthNSection = Configuration.GetSection("Authentication:Google");
options.ClientId = googleAuthNSection["ClientId"];
options.ClientSecret = googleAuthNSection["ClientSecret"];
options.Scope.Add("profile");
options.Events.OnCreatingTicket = (context) =>
{
var picture = context.User.GetProperty("picture").GetString();
context.Identity.AddClaim(new Claim("picture", picture));
return Task.CompletedTask;
};
});
Run Code Online (Sandbox Code Playgroud)
我发现无法从声明中获取图片网址。最后我找到了一个使用 nameidentifier 的解决方案,它附带了声明。
string googleApiKey = "{your google api key}";
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);
}
Run Code Online (Sandbox Code Playgroud)
您只需要一个 Google API 密钥即可。
要创建 API 密钥:
A。在 Google API 列表中,搜索 Google+ API 服务。
b. 从结果列表中选择 Google+ API。
C。按启用 API 按钮。
该过程完成后,Google+ API 将显示在已启用的 API 列表中。要访问,请选择左侧边栏菜单上的 API 和服务,然后选择启用的 API 选项卡。
https://developers.google.com/+/web/api/rest/oauth
| 归档时间: |
|
| 查看次数: |
2185 次 |
| 最近记录: |