如何使用ASP.NET MVC 5访问Facebook配置文件信息

时间:2015-07-23 10:10:26

标签: c# asp.net asp.net-mvc facebook asp.net-identity

我正在ASP.NET MVC 5中开发一个基本网站(使用visual studio 2013)。该网站将使用Facebook进行用户身份验证和检索初始个人资料数据。像堆栈流量登录与facebook ..access用户名,个人资料照片等..plz任何一个帮助我相关的样本

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);

帐户控制器>> Externallogincallback

   // 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>