Web API facebook,电子邮件声明返回null

时间:2016-10-21 09:37:13

标签: c# facebook-graph-api asp.net-web-api registration claims-based-identity

代码如下

startup auth:

        app.UseFacebookAuthentication(new FacebookAuthenticationOptions
        {
            AppId = "xxx",
            AppSecret = "xxx",
            BackchannelHttpHandler = new FacebookBackChannelHandler(),
            UserInformationEndpoint = "https://graph.facebook.com/v2.4/me?fields=id,name,email,first_name,last_name,location",
            Scope = { "email" }
        });
在accountcontroler RegisterExternal类中的

我调用以下内容:

var info = await AuthenticationManager_GetExternalLoginInfoAsync_WithExternalBearer();

这是什么课程:

private async Task<ExternalLoginInfo> AuthenticationManager_GetExternalLoginInfoAsync_WithExternalBearer()
    {
        ExternalLoginInfo loginInfo = null;

        var result = await Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ExternalBearer);

        if (result != null && result.Identity != null)
        {
            var idClaim = result.Identity.FindFirst(ClaimTypes.NameIdentifier);
            if (idClaim != null)
            {
                loginInfo = new ExternalLoginInfo()
                {
                    DefaultUserName = result.Identity.Name == null ? "" : result.Identity.Name.Replace(" ", ""),
                    Login = new UserLoginInfo(idClaim.Issuer, idClaim.Value),
                };

            }
        }
        return loginInfo;
    }

这是因为默认情况下RegisterExternal类将使用身份验证类型cookie。每当我使用它时它将返回null,所以在浏览网页后我发现有必要添加此代码,而这又将使用承载进行身份验证,这将导致回复,其中用户名和身份为否更长的空,因此被授权。 (见下图)

Return object (username and login)

但是,当我想要发送电子邮件时,我不能这样做。无论我做什么,它总是会返回null。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,并通过使用Facebook nuget包来获得额外的字段。

在我的应用程序中,我实现了<?php include "PHPMailer_5.2.4/class.phpmailer.php"; $mail = new PHPMailer; $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->SMTPAuth = true; $mail->Username = 'test@gmail.com'; $mail->Password = 'test@12123#'; $mail->SMTPSecure = 'ssl'; $mail->Port = 465; $mail->addAddress('abc@gmail.com'); $mail->setFrom('abc@gmail.com'); $mail->Subject = 'Test'; $mail->Body = 'Testing'; if($mail->send()) { echo 'Message has been sent'; } else { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } ?> 并使用以下内容覆盖了FacebookAuthenticationProvider方法:

Authenticated(...)

我的public class FacebookAuthProvider : FacebookAuthenticationProvider { public override Task Authenticated(FacebookAuthenticatedContext context) { var accessTokenClaim = new Claim("ExternalAccessToken", context.AccessToken, "urn:facebook:access_token"); context.Identity.AddClaim(accessTokenClaim); var extraClaims = GetAdditionalFacebookClaims(accessTokenClaim); context.Identity.AddClaim(new Claim(ClaimTypes.Email, extraClaims.First(k => k.Key == "email").Value.ToString())); context.Identity.AddClaim(new Claim("Provider", context.Identity.AuthenticationType)); context.Identity.AddClaim(new Claim(ClaimTypes.Name, context.Identity.FindFirstValue(ClaimTypes.Name))); var userDetail = context.User; var link = userDetail.Value<string>("link") ?? string.Empty; context.Identity.AddClaim(new Claim("link", link)); context.Identity.AddClaim(new Claim("FacebookId", userDetail.Value<string>("id"))); return System.Threading.Tasks.Task.FromResult(0); } private static JsonObject GetAdditionalFacebookClaims(Claim accessToken) { var fb = new FacebookClient(accessToken.Value); return fb.Get("me", new { fields = new[] { "email", "first_name", "last_name" } }) as JsonObject; } } Startup.cs内有此内容:

Configuration(IAppBuilder app)

我不知道为什么电子邮件首先没有填充,但这种方法对我有用。