从ASP.Net核心Facebook身份验证

时间:2017-03-08 04:15:00

标签: asp.net-mvc asp.net-core asp.net-identity facebook-login asp.net-core-identity

我使用实现Facebook登录(以及其他提供商)的默认模板时遇到了麻烦。 我可以登录到应用程序,但是登录后我想要登记表格,预先填写Facebook数据(以完成个人资料)。 我没有问题来获取默认声明(电子邮件,姓名),但我无法添加生日或地点。

我的创业公司有以下几行:

fo.Scope.Add("public_profile");
fo.Scope.Add("email");
fo.Scope.Add("user_birthday");
fo.Scope.Add("user_location");
...
app.UseFacebookAuthentication(fo);

这有效,但在登录过程中不要求这些权限。 如果我添加

fo.Fields.Add("location"); or 
fo.Fields.Add("user_birthday");

回调中的登录过程失败。

外部登录电话是默认电话:

var redirectUrl = Url.Action("ExternalLoginCallback", Name, new { ReturnUrl = returnUrl });
var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
return Challenge(properties, provider);

回调如下:

        var info = await _signInManager.GetExternalLoginInfoAsync();
        if (info == null)
            return RedirectToAction(nameof(Login));

        // Sign in the user with this external login provider if the user already has a login.
        var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false);
        if (result.Succeeded)
        {
            _logger.LogInformation(5, "User logged in with {Name} provider.", info.LoginProvider);
            return RedirectToLocal(returnUrl);
        }
        if (result.RequiresTwoFactor)
            return RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl });
        if (result.IsLockedOut)
            return View("Lockout");
        else
        {
            // If the user does not have an account, then ask the user to create an account.
            ViewData["ReturnUrl"] = returnUrl;
            ViewData["LoginProvider"] = info.LoginProvider;

            Console.WriteLine("Debug claims!");
            foreach (var c in info.Principal.Claims)
                Console.WriteLine("---> " + c.Subject + ":" + c.Value + "   " + c.ToString());
            Console.WriteLine("End Debug claims!");

我从来没有得到任何超过默认声明的内容,并且在启动时添加了字段时出现了类似的例外情况

  An unhandled exception has occurred: Response status code does not indicate success: 400 (Bad Request).
System.Net.Http.HttpRequestException: Response status code does not indicate success: 400 (Bad Request).
at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
at Microsoft.AspNetCore.Authentication.Facebook.FacebookHandler.<CreateTicketAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Authentication.OAuth.OAuthHandler`1.<HandleRemoteAuthenticateAsync>d__5.MoveNext()

有关如何做到这一点的任何想法? (不是通过图形API,即使它更容易) 我一直在寻找几个小时,除了&#34;电子邮件&#34;的问题。

这两个问题是: 1.为什么在登录时,不需要启动时设置的范围? 2.如何从Facebook上搜索字段? (生日,性别,地点)

0 个答案:

没有答案