姓氏和电子邮件地址

姓氏和电子邮件地址

本文介绍了使用ASP.NET从Facebook获取名字,姓氏和电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Startup.Auth.cs文件中包含以下代码,但是我找不到从Facebook获取名字和姓氏的方法:

I have the below code in my Startup.Auth.cs file but I can't find a way to get the firstname and lastname from Facebook:

var facebookOptions = new FacebookAuthenticationOptions()
{
    AppId = "",
    AppSecret = "",
    BackchannelHttpHandler = new FacebookBackChannelHandler(),
    UserInformationEndpoint = "https://graph.facebook.com/v2.4/me?fields=id,email"
};

facebookOptions.Scope.Add("email");
facebookOptions.Scope.Add("public_profile");

app.UseFacebookAuthentication(facebookOptions);

推荐答案

下面是一个快速示例,说明如何在asp.net winforms中从Facebook获取用户信息.此示例使用适用于.Net的Facebook SDK .此代码段使用Facebook登录用户,然后获取了额外的用户信息,但也可以仅用于获取额外的个人资料数据.

Here is a quick example of how you can get the user information from Facebook in asp.net winforms. This example uses the Facebook SDK for .Net.This snippet logs in the user with Facebook and then gets the extra user info, but can also be used for just getting the extra profile data.

static string Facebook_Key = "yourAppID";
static string Facebook_Secret = "yourAppSecret";

protected void Page_Load(object sender, EventArgs e)
{
    //check if the user is returning from Facebook
    if (Request.QueryString["code"] != null)
    {
        FacebookClient fbc = new FacebookClient();

        //retreive the access token
        dynamic result = fbc.Post("oauth/access_token", new
        {
            client_id = Facebook_Key,
            client_secret = Facebook_Secret,
            redirect_uri = Request.Url.AbsoluteUri,
            code = Request.QueryString["code"].ToString()
        });

        fbc.AccessToken = result.access_token;

        //get the extra profile data
        dynamic user = fbc.Get("me?fields=first_name,last_name,id,email");

        //display the results
        FacebookLoginLabel.Text += user.id + "<br>";
        FacebookLoginLabel.Text += user.first_name + "<br>";
        FacebookLoginLabel.Text += user.last_name + "<br>";
        FacebookLoginLabel.Text += user.email + "<br>";

        //start the actual aspnet registration or login
        LoginOrRegisterUser();
    }
}

protected void FacebookLoginButton_Click(object sender, EventArgs e)
{
    FacebookClient fbc = new FacebookClient();

    //start the Facebook login request
    var loginUrl = fbc.GetLoginUrl(new
    {
        client_id = Facebook_Key,
        redirect_uri = Request.Url.AbsoluteUri,
        response_type = "code",
        scope = "email"
    });

    //redirect the user to Facebook for authentication
    Response.Redirect(loginUrl.AbsoluteUri);
}

ASPX

<asp:Button ID="FacebookLoginButton" runat="server" Text="Log in with Facebook" OnClick="FacebookLoginButton_Click" />

<asp:Label ID="FacebookLoginLabel" runat="server" Text=""></asp:Label>

这篇关于使用ASP.NET从Facebook获取名字,姓氏和电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 04:30