Facebook登录返回ASP.NET MVC中的失败

时间:2016-05-28 19:15:44

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

我正在尝试在我的ASP.NET MVC应用程序中实现FaceBook登录。

每次尝试运行SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false)时,都会返回Failure

这是代码 -

Startup.Auth.cs

app.UseFacebookAuthentication(
   appId: Social_NetworkBLL.FaceBook_AppID,
   appSecret: Social_NetworkBLL.FaceBook_SecretID
);

的AccountController

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
    // Request a redirect to the external login provider
    return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
}

[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
    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:
            return RedirectToAction("Login");
        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 });

            ExternalLoginConfirmationViewModel vm = new ExternalLoginConfirmationViewModel { Email = loginInfo.Email };
            //return RedirectToAction("ExternalLoginConfirmation", new { model = vm, returnUrl = returnUrl });
            return await ExternalLoginConfirmation(vm, returnUrl);
    }
}

FB App设置 enter image description here

1 个答案:

答案 0 :(得分:1)

SignInStatus.Failure表示用户是否已在您的网站上注册。默认的ASP.NET MVC帐户控制器的ExernalLoginCallBack看起来像这样。

 //
        // GET: /Account/ExternalLoginCallback
        [AllowAnonymous]
        public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
        {
            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 });
            }
        }

在上面的代码中,您将重定向到登录页面 Case SignInStatus.Failure.删除RedirectToAction("Login");