ClaimTypes用于在mvc 6中获取用户的个人资料图像

时间:2017-04-25 04:58:01

标签: c# asp.net-mvc oauth

这是我的<div page-directive my-template="templateA" my-controller="controllerA"> </div> <div page-directive my-template="templateA" my-controller="controllerB"> </div> 任务,也就是创建项目时添加的默认代码。我添加了这些行以获得用户声明:

ExternalLoginConfirmation

但我无法获取用户个人资料图片,因为user.Firstname = info.Principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.GivenName)?.ToString(); user.Lastname = info.Principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Surname)?.ToString(); user.Gender = info.Principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Gender)?.ToString(); 中不存在该图片。

ClaimTypes

请注意, public async Task<IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl = null) { if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { return View("ExternalLoginFailure"); } var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { user.Firstname = info.Principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.GivenName)?.ToString(); user.Lastname = info.Principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Surname)?.ToString(); user.Gender = info.Principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Gender)?.ToString(); await _signInManager.SignInAsync(user, isPersistent: false); _logger.LogInformation(6, "User created an account using {Name} provider.", info.LoginProvider); return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewData["ReturnUrl"] = returnUrl; return View(model); } 缺少info,并且以下功能也不可用:

ExternalPrincipal

1 个答案:

答案 0 :(得分:1)

这是我现在使用的灵魂:

 var result = await _userManager.CreateAsync(user);
       if (result.Succeeded)
       {
            result = await _userManager.AddLoginAsync(user, info);
            if (result.Succeeded)
            {
                 if (info.LoginProvider.ToLower().IndexOf("google") != -1)
                    { await _userManager.AddClaimAsync(user, new Claim("GooglePlusId", info.ProviderKey));
                        try {
                                HttpClient client = new HttpClient();
                                HttpResponseMessage x = await client.GetAsync($"https://www.googleapis.com/plus/v1/people/{info.ProviderKey}?fields=image&key=YOUR_GOOGLE_PLUS_API_KEY");
                               dynamic img = Newtonsoft.Json.JsonConvert.DeserializeObject(await x.Content.ReadAsStringAsync());
                                user.PhotoLink = img.image.url;
                                db.SaveChanges();
                            }
                            catch { }

                    }

                        if (info.LoginProvider.ToLower().IndexOf("facebook") != -1)
                        {
                            user.PhotoLink = $"http://graph.facebook.com/{info.ProviderKey}/picture?type=square&width=50";
                        }   
                        await _signInManager.SignInAsync(user, isPersistent: false);
                        _logger.LogInformation(6, "User created an account using {Name} provider.", info.LoginProvider);
                        return RedirectToLocal(returnUrl);
                    }
                }

P.S。:此代码应添加到ExternalLoginConfirmation控制器

中的AccountControllers操作中