HI WELCOME TO KANSIRIS

Facebook login returns Failure in ASP.NET MVC

Leave a Comment
I am trying to implement FaceBook login in my ASP.NET MVC app.
Everytime it tries to run SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false), it returns Failure
Here's the code-
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 Settings enter image description here
SignInStatus.Failure indicates whether the user is already registered on your site or not. The default ASP.NET MVC Account Controller's ExernalLoginCallBack looks like this.
 //
        // 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 });
            }
        }
In you code above, you are redirecting to the Login Page on Case SignInStatus.Failure. Remove the RedirectToAction("Login");

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.