Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 5 (VS2013 final): Facebook login with OWIN fails (loginInfo is null)

I installed the VS2013 final bits that were released yesterday, and I'm trying to get an example working where I enable an external Facebook login. My first question:

In this controller code (which I did not touch and left as is from the sample template):

    //
    // GET: /Account/ExternalLoginCallback
    [AllowAnonymous]
    public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
    {
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        if (loginInfo == null)
        {
            return RedirectToAction("Login");
        }

I set a breakpoint on the line await AuthenticationManager.GetExternalLoginInfoAsync(). The code returns (after I do my Facebook login), and "loginInfo" is null. In the UI, the login page continues to be displayed, with no change. How can I debug this? I was trying to find the code inside of GetExternalLoginInfoAsync() but according to this thread:

Where is Microsoft.AspNet.Identity.Owin.AuthenticationManager in Asp.Net Identity RTM version?

AuthenticationManager is now gone. (This does not seem to be the case per above.)

My second question: Is anyone else able to get the sample working with Facebook login with no changes to the ASP.NET MVC5 sample code (other than uncommenting app.UseFacebookAuthentication and filling in your FB app details)? (You'll have to configure an alias host with Facebook, such as "localtest.me" and configure it with IIS express.)

Thanks for any help...

-Ben

like image 295
BenjiFB Avatar asked Oct 18 '13 15:10

BenjiFB


4 Answers

I had this problem while authenticating to Google (OpenID , not OAuth). So there really was nothing to mess up, but sure enough it stopped working after some simple code changes around session variables. Using fiddler I determined that Google was indeed returning valid information.

Someone smarter than me figured out my issue and describes the fix here.

ASP.NET_SessionId + OWIN Cookies do not send to browser

All I had to do was add Session["salt"] = "salt"; to the Account/Login controller, and viola it magically started working perfectly. Not sure if this is your exact issue, but this was mine.

like image 183
MarloBello Avatar answered Nov 19 '22 10:11

MarloBello


Ok, this was silly of me. First off, I started debugging using Fiddler, and saw that Facebook was sending back this:

WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "The request is invalid because the app secret is the same as the client token"

This message was inaccurate, but searching for it, I found another thread where a user specified that resetting the app secret solved the problem. (I hadn't used this app in a long time so that may have been related.) So I took those steps, but still saw the problem. But the message in Fiddler changed:

WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "The request is invalid because the app is configured as a desktop app"

Indeed, the app was misconfigured in Facebook. I set it to be a web app, and it works perfectly now! Hopefully this can help others, as this was not clear until firing up Fiddler.

like image 28
BenjiFB Avatar answered Nov 19 '22 11:11

BenjiFB


I started getting this in the latest VS 2013.3 template and realized the authentication wasn't playing nice with FormsAuthentication that I unnecessarily ported from one of my other projects. Here's what I did to fix it:

added <system.web><authentication mode="None" />...

added <system.webServer><modules><remove name="FormsAuthentication" /></modules>...

like image 2
parliament Avatar answered Nov 19 '22 10:11

parliament


For me this failed because I assumed that facebook always has access to birthday property, but this is not the case:

// facebook doesn't guarantee that this value exists always,
// thus we can't really add this claim to our identity object, if it's null.
var birthday = context.User.Value<string>("birthday");
if (birthday != null)
{
    context.Identity.AddClaim(new Claim("Birthdate",
         context.User.Value<string>("birthday")));
}

This extends to pretty much everything:

1) some of the users don't have last name (allowed)

2) some of the users don't have email in fb (allowed)

3) some of the users don't give out birthday,

You have to take care of everything.

like image 2
Erti-Chris Eelmaa Avatar answered Nov 19 '22 11:11

Erti-Chris Eelmaa