我在Startup.Auth.cs文件中有以下代码,但我找不到从Facebook获取名字和姓氏的方法:
:
答案 0 :(得分:2)
以下是如何在asp.net winforms中从Facebook获取用户信息的快速示例。此示例使用Facebook SDK for .Net。 此代码段使用Facebook登录用户,然后获取额外的用户信息,但也可用于获取额外的个人资料数据。
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>