如何使用ASP.NET MVC 5访问Facebook个人资料信息

4

我正在使用Visual Studio 2013开发一个基本的ASP.NET MVC 5网站。该网站将使用Facebook进行用户认证,并检索初始配置文件数据。类似于在Stack Overflow中使用Facebook登录,访问用户名、个人资料照片等。请帮忙提供相关示例。

startup.auth.cs

 FacebookAuthenticationOptions fbao = new FacebookAuthenticationOptions();
        fbao.AppId = "****";
        fbao.AppSecret = "*****";
        fbao.Scope.Add("email");
        fbao.Scope.Add("user_birthday");
        fbao.Scope.Add("user_hometown");

        fbao.SignInAsAuthenticationType = Microsoft.Owin.Security.AppBuilderSecurityExtensions.GetDefaultSignInAsAuthenticationType(app);

        app.UseFacebookAuthentication(fbao);

账户控制器>>外部登录回调

   // GET: /Account/ExternalLoginCallback
    [AllowAnonymous]
    public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
    {
        ClaimsIdentity fboa = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
        //var email = ext.Claims.First(x => x.Type.Contains("emailaddress")).Value;
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        if (loginInfo == null)
        {
            return RedirectToAction("Login");
        }

        // Sign in the user with this external login provider if the user already has a login
        var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);



        switch (result)
        {
            case SignInStatus.Success:
                return RedirectToLocal(returnUrl);
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
            case SignInStatus.Failure:
            default:
                // If the user does not have an account, then prompt the user to create an account
                ViewBag.ReturnUrl = returnUrl;
                ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email });
        }
    }

账户视图模型

public class ExternalLoginConfirmationViewModel
{
    [Required]
    [Display(Name = "Email")]
    public string Email { get; set; }
    public string UserName { get; set; }
}

externalloginconfirmationview

<h4>Association Form</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<p class="text-info">
    You've successfully authenticated with <strong>@ViewBag.LoginProvider</strong>.
    Please enter a user name for this site below and click the Register button to finish
    logging in.
</p>
<div class="form-group">
    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.UserName, "", new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" class="btn btn-default" value="Register" />
    </div>
</div>

嗨,Surya,像这样在这里提问有时会吸引那些不愿帮助你而是打击你的人。它没有提供任何关于你是否尝试过任何事情的信息,很多人可能会认为你是在要求别人为你做这件事。这样的问题也可能会被贬低,因为需要一个巨大的答案来解释。试着在谷歌上搜索一些简单的东西,比如“如何使用asp.net登录facebook”。我刚刚尝试了一下,找到了相当多的结果。 - Keithin8a
但是我的问题不是Facebook登录问题...个人资料细节无法访问数据库..像这样类型的示例更常检查..但还没有正确的解决方案。 - Aysha Parveen Pallikkara
理想情况下,您应该将所有内容都放在问题中。请放置您已经拥有的代码以及未返回数据的位置。请记住,在编程时,一个错误可能来自许多不同的地方,因此如果您可以提供尽可能多的相关信息或代码,那么人们将非常愿意帮助您。 - Keithin8a
这个问题的最佳解决方案是仅使用访问令牌的Facebook Graph API。 - Ravi Anand
1个回答

2

我的实际问题是假设我的网站是StackOverflow,我用一个新用户登录,然后点击Facebook登录按钮,登录后访问我们的个人资料信息,如用户名、个人资料照片、出生日期等。你提供的链接是ASP.NET Facebook应用程序创建,但我的问题实际上是Web应用程序已经实现了Facebook登录,登录成功,但没有将数据检索到数据库中。 - Aysha Parveen Pallikkara
将数据保存到数据库中是相当简单的任务。我想你已经创建了必要的表格来保存从Facebook返回的数据了吧?如果是这样,你能分享一下调用Facebook API并返回数据的C#语句,以及尝试将数据保存在数据库中的后续语句吗?如果数据无法保存,它会引发什么样的问题/异常,这将非常有帮助。 - ItsZeus
我之前分享的这个链接展示了如何将数据保存在数据库中。如果你按照这个教程,它会解释如何保存从Facebook返回的数据到数据库中。http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on#1st - ItsZeus
请访问此链接:http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on。这是一个关于如何访问电子邮件的示例,我已经获得了电子邮件权限。 - Aysha Parveen Pallikkara
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接