AspNetKatana icon indicating copy to clipboard operation
AspNetKatana copied to clipboard

[Announcement] Facebook 3.0.1 and lower no longer work

Open Tratcher opened this issue 9 years ago • 78 comments

Facebook as deprecated their old OAuth endpoints that were used by Katana 3.0.1 and lower. Here's a Fiddler trace of a failing auth flow:

#	Result	Protocol	Host	URL	Body	Caching	Content-Type	Process	Comments	Custom	
1191	302	HTTPS	localhost:44318	/Account/ExternalLogin	0	private		chrome:16236			
1202	302	HTTPS	www.facebook.com	/dialog/oauth?response_type=code&client_id=569522623154478&redirect_uri=https%3A%2F%2Flocalhost%3A44318%2Fsignin-facebook&scope=&state=gQ2fRAt8BI46eC52Z_YdSFMCYbBleCGJO5Jl1BO4yQQFl0dVjx-Z0EqMS6QGNfIHD6n7fApnqdodg6ea4E7Ky9rsnExnoW22a7mV7uYAnj089d3yKm6TN4F2YoFgeVPZPakdddB_D-b8988omDTjeQPHrfSVNFqqATAsvab15PHkSaCuk5OqWZRJUnkKtfanM2uA9E8PH4_JrNrLc4DZyd0tRfGr0C3aHUkPJMUiEq0	0	private, no-cache, no-store, must-revalidate; Expires: Sat, 01 Jan 2000 00:00:00 GMT	text/html	chrome:16236			
1203	302	HTTPS	localhost:44318	/signin-facebook?code=AQDQ5k6PZ623JZqcDBHkeK6-uryTkyuKZWGD5Hk_rcLV2sYArbQKJE-d-WIvHGikif_5VtclZojZUInsOmV_KEYUYD9jL3Gn0qKKzdk6574_Ya8IoAVPpvm9hsHZ50bKAxItn01fTW54hhGRxUFpX4yCLqXrLtVgImctDxM73XGTEq7poHN7nglEcU0TjFKImpc4Pu-FkkrQXoDGrD4Xeig4NCJHzvJcw8Oc8iJIBCJDSESi6Y2U5Y2Gsy4WntIkRnhCPqh3p--h_2LFqbRcjpx9KYrvPqhW9sr5eShwY1JJ8fVlKzBQmmQbgf0IvTcbckLnxJOPCsIyIFCs5xNf9AH4&state=gQ2fRAt8BI46eC52Z_YdSFMCYbBleCGJO5Jl1BO4yQQFl0dVjx-Z0EqMS6QGNfIHD6n7fApnqdodg6ea4E7Ky9rsnExnoW22a7mV7uYAnj089d3yKm6TN4F2YoFgeVPZPakdddB_D-b8988omDTjeQPHrfSVNFqqATAsvab15PHkSaCuk5OqWZRJUnkKtfanM2uA9E8PH4_JrNrLc4DZyd0tRfGr0C3aHUkPJMUiEq0	0			chrome:16236			
1205	200	HTTPS	graph.facebook.com	/oauth/access_token?grant_type=authorization_code&code=AQDQ5k6PZ623JZqcDBHkeK6-uryTkyuKZWGD5Hk_rcLV2sYArbQKJE-d-WIvHGikif_5VtclZojZUInsOmV_KEYUYD9jL3Gn0qKKzdk6574_Ya8IoAVPpvm9hsHZ50bKAxItn01fTW54hhGRxUFpX4yCLqXrLtVgImctDxM73XGTEq7poHN7nglEcU0TjFKImpc4Pu-FkkrQXoDGrD4Xeig4NCJHzvJcw8Oc8iJIBCJDSESi6Y2U5Y2Gsy4WntIkRnhCPqh3p--h_2LFqbRcjpx9KYrvPqhW9sr5eShwY1JJ8fVlKzBQmmQbgf0IvTcbckLnxJOPCsIyIFCs5xNf9AH4&redirect_uri=https%3A%2F%2Flocalhost%3A44318%2Fsignin-facebook&client_id=xxxxx&client_secret=xxxxxxxx	251	private, no-cache, no-store, must-revalidate; Expires: Sat, 01 Jan 2000 00:00:00 GMT	application/json; charset=UTF-8	iisexpress:1144			
1206	302	HTTPS	localhost:44318	/Account/ExternalLoginCallback?error=access_denied	442	private	text/html; charset=utf-8	chrome:16236			
1207	200	HTTPS	localhost:44318	/Account/Login	2,134	private	text/html; charset=utf-8	chrome:16236			

Note the /Account/ExternalLoginCallback?error=access_denied request.

If you enable logging here's the message (caused by a change in Facebook's response format): https://github.com/aspnet/AspNetKatana/wiki/Debugging#logging

Microsoft.Owin.Security.Facebook.FacebookAuthenticationMiddleware Error: 0 : Authentication failed
System.ArgumentNullException: Value cannot be null.
Parameter name: stringToEscape
   at System.Uri.EscapeDataString(String stringToEscape)
   at Microsoft.Owin.Security.Facebook.FacebookAuthenticationHandler.<AuthenticateCoreAsync>d__0.MoveNext()
    ProcessId=1144
    DateTime=2017-03-27T20:53:38.6034000Z

This has been fixed in Katana 3.1.0-RC1 which is now available on nuget.org.

Tratcher avatar Mar 27 '17 21:03 Tratcher

Thanks for this, noticed this wasn't working this morning. Looks like a few other sites have been hit by this also.

refactorthis avatar Mar 27 '17 23:03 refactorthis

This stopped working also for my website

arnoldsi-tr avatar Mar 28 '17 09:03 arnoldsi-tr

This saved me even more pain. I noticed that Facebook Login on my site stopped to work two days ago. After hours of investigating an error that came out of nowhere, installing 3.1.0 RC1 resolved the issue. The middleware now handles the JSON response properly.

embee8 avatar Mar 28 '17 17:03 embee8

Been debugging this myself today. The problem is in FacebookAuthenticationHandler line 90: ... [request to https://graph.facebook.com/oauth/access_token here] 88: tokenResponse.EnsureSuccessStatusCode(); 89: string text = await tokenResponse.Content.ReadAsStringAsync(); 90: IFormCollection form = WebHelpers.ParseForm(text);

The code make a request to https://graph.facebook.com/oauth/access_token and then tries to parse the response and ParseForm method expects a parameter with query string like format - key1=value&key2=value , but now after facebook API changes the response comes as a json - { "key1": "value", "key2":"value"}, so the access_token cannot be extracted any more and the whole world crashes silently.

It looks like an easy fix, any chance someone will update the repo and push a new version to nugget? Would do it myself but....

Andrei- avatar Mar 28 '17 20:03 Andrei-

@Andrei, you can get the updated version at https://www.nuget.org/packages/Microsoft.Owin.Security.Facebook/3.1.0-rc1, or go to Tools > nuget package manager > manage packets for solution, click on "updates" and check "include prerelease".

Cheers

devictor47 avatar Mar 28 '17 21:03 devictor47

@wizzar, awesome, thanks.

Andrei- avatar Mar 28 '17 21:03 Andrei-

This doesn't seem to be fixed. I still get the same error with Facebook.

paul-turner-github avatar Mar 29 '17 04:03 paul-turner-github

Still not working, I updated to the latest version 3.1.0-rc1 and made sure all DLLs got updated,

I'm using it like this:


var facebookAuthOptions = new FacebookAuthenticationOptions();
facebookAuthOptions.AppId = "xxx";
facebookAuthOptions.AppSecret = "yyy";
facebookAuthOptions.SendAppSecretProof = true;
facebookAuthOptions.CallbackPath = new PathString("/signin-facebook");

facebookAuthOptions.Scope.Add("public_profile");
facebookAuthOptions.Scope.Add("email");
facebookAuthOptions.Scope.Add("user_birthday");

// added for Microsoft.Owin.Security.Facebook/3.1.0-rc1
facebookAuthOptions.Fields.Add("email");
facebookAuthOptions.Fields.Add("birthday");
facebookAuthOptions.Fields.Add("gender");
facebookAuthOptions.Fields.Add("locale");
facebookAuthOptions.Fields.Add("location");
facebookAuthOptions.Fields.Add("timezone");

facebookAuthOptions.Provider = new FacebookAuthenticationProvider()
{
    OnAuthenticated = (context) =>
    {
        context.Identity.AddClaim(new Claim("FacebookAccessToken", context.AccessToken));

        var expiryDuration = context.ExpiresIn ?? new TimeSpan();
        context.Identity.AddClaim(new Claim("urn:facebook:expires_in", DateTime.UtcNow.Add(expiryDuration).ToString(CultureInfo.InvariantCulture)));

        // Add all other available claims
        foreach (var claim in context.User)
        {
            var claimType = string.Format("urn:facebook:{0}", claim.Key);
            var claimValue = claim.Value.ToString();
            if (!context.Identity.HasClaim(claimType, claimValue))
                context.Identity.AddClaim(new Claim(claimType, claimValue, "XmlSchemaString", "Facebook"));
        }

        return Task.FromResult(0);
    }
};
app.UseFacebookAuthentication(facebookAuthOptions);

Any ideas?

UPDATE: I spent hours on this today, not resolved yet, I deleted my facebook web app (it was API 2.4) and created a new one (2.8) that is not approved yet, but I think test app should be working, so far I'm getting a 302 error=access_denied

UPDATE 2: new facebook app is approved, still not working. not even without using the FacebookAuthenticationProvider

The net traffic looks like this:

https://localhost:44300/external-providers (302) https://www.facebook.com/v2.8/dialog/oauth?response_type=code&client_id=1897713540512505&redirect_uri=https%3A%2F%2Flocalhost%3A44300%2Fsignin-facebook&scope=&state=OXOiCtOJPEK5WCQkEJt-mH3lxEnTCXG3YcGFtW7j_dyuPd_9LevphavHobpB6EH3Wfb8BpbhqST13SacFMB3OnddsgnslzcAX7KnMpzpnEcfFI3KErmByenV9xn1IEt7PhY4TjXi5KHFNUp1PVu_ikUBnvf0hJpY2IK0fczvQLVmGKplJV-g6qsEMYXfKHNYoqjDiu9cA-iGIPXfRxPxZg (302) https://localhost:44300/signin-facebook?code=AQDrGJ9sqk0ENVw_7RW3zmauzV1Gf6rWXBotgAKf6uBbdryamL5RMHiOgIrBiPYdy8CA9HC9hq2sahu2bt-VwznOJ8PNFTxRmLCe0PRrwxSsffscHSdNzjz16jjavuupse087mB_2KhZeKKWeS2Rlwabcfa-n4FGIcyDizpOKTWJl_J6PFAjWv3XfPy1zkbLz0cAsTdQ8t8pOARPhP5nKwUIGN5GuS4fzuGhgPCerlPyHknK0BWcFrRC0fa_n3X2d0-zANu6nzhRspssDTWmIAr9PZCLOlpSMlJeb1NrebECNbw4YqGzScCWxBRVL8YAfQ4&state=OXOiCtOJPEK5WCQkEJt-mH3lxEnTCXG3YcGFtW7j_dyuPd_9LevphavHobpB6EH3Wfb8BpbhqST13SacFMB3OnddsgnslzcAX7KnMpzpnEcfFI3KErmByenV9xn1IEt7PhY4TjXi5KHFNUp1PVu_ikUBnvf0hJpY2IK0fczvQLVmGKplJV-g6qsEMYXfKHNYoqjDiu9cA-iGIPXfRxPxZg (302) https://localhost:44300/signup-connect?error=access_denied (302) https://localhost:44300/signout (200)

YovavGad avatar Mar 29 '17 19:03 YovavGad

@YovavGad, it seems that 3.1.0 RC1 has fixed it for most people in this thread, so I assume it comes down to your personal configuration. Remember this is a place to track issues, not to troubleshoot custom setups. If you think the latest release doesn't work as intended, it might be useful for the project owners to provide additional information, i.e. what do you mean with "still not working"? Some debug logs would be helpful to further investigate the issue.

Tip: Try it without specifying the Provider. After upgrading to 3.1.0 RC1, I literally went back to the most basic setup where I only specify the App ID, Secret, and UserInformationEndpoint and it worked.

In your case: facebookAuthOptions.UserInformationEndpoint = "https://graph.facebook.com/v2.8/me?fields=email,first_name,last_name[etc]";

embee8 avatar Mar 29 '17 21:03 embee8

@embee8 Fields can be specified on Options now, just like Scope.

Tratcher avatar Mar 29 '17 21:03 Tratcher

Fantastic news, @Tratcher! What I have struggled with in the past is to find documentation of all the different options. The boilerplate code in the standard MVC template only outlined the app secret and id. Is there an official and complete documentation out there?

embee8 avatar Mar 29 '17 21:03 embee8

@embee8 nothing extensive, start with the Options doc comments. https://github.com/aspnet/AspNetKatana/blob/f3a8d848b7548e62d52737efa0a4fc9cf6c6cedf/src/Microsoft.Owin.Security.Facebook/FacebookAuthenticationOptions.cs#L138

Tratcher avatar Mar 29 '17 21:03 Tratcher

Or MSDN: https://msdn.microsoft.com/en-us/library/microsoft.owin.security.facebook.facebookauthenticationoptions(v=vs.113).aspx

Tratcher avatar Mar 29 '17 21:03 Tratcher

Hi,

I use webpages and use the following Microsoft.Web.WebPages.OAuth and call function OAuthWebSecurity.VerifyAuthentication to verify facebook logins.

In Nuget it is still 3.2.3 dated 9th February 2015.

what can be the solution please?

arinray73 avatar Mar 30 '17 00:03 arinray73

@NTaylorMullen @Eilon ?

Tratcher avatar Mar 30 '17 03:03 Tratcher

@arinray73 that package ultimately uses the 3rd-party DotNetOpenAuth library to do all the actual OAuth protocol implementation. Can you share more info about what isn't working for you?

Eilon avatar Mar 30 '17 04:03 Eilon

I can confirm that the 3.1.0-rc1 works now if you remove the custom FacebookAuthenticationProvider.

My config looks like this now. I have added email in scope.

var facebookOptions = new FacebookAuthenticationOptions
{
  AppId = ConfigurationManager.AppSettings["FacebookAppId"],
  AppSecret = ConfigurationManager.AppSettings["FacebookAppSecret"],
};
facebookOptions.Scope.Add("email");
app.UseFacebookAuthentication(facebookOptions);

erzki avatar Mar 30 '17 06:03 erzki

Greetings,

Just updated Microsoft.Owin.Security.Facebook package to 3.1.0-rc1. I have very simple setup based on Microsoft example for external authentication:

var fbAuthOptions = new FacebookAuthenticationOptions();
fbAuthOptions.Scope.Add("email");
fbAuthOptions.AppId = "111111111111111";
fbAuthOptions.AppSecret = "secret_here";
 // Enable the application to use a cookie to store information for the signed in user
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login")
});

app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseFacebookAuthentication(fbAuthOptions);

All goes fine according to Fiddler:

GET https://www.facebook.com/v2.8/dialog/oauth?response_type=code&client_id=111111111111111&redirect_uri=http%3A%2F%2Flocalhost%3A59735%2Fsignin-facebook&scope=email&state=v5WWRCHT93gXIGgkEYOtg57yD7BI7dhu6okwao41f8kZzgfcE5Zyq3W0QW2QmfMb-RXFuaovmNkL0wuP1RiLefnIOWkOaf4dkaHTo0C69V9aNn59KYcEd4z4fbyRct4g1jfb0OLYvHuaO6sDAcxejUTZiTIuC4gy4za-H6LsHdrETDsskb8iu2PQXZbR-4bzzn9j2H1rshS6AxN8xKuqFvKEABZ1OeJS2O4qKrxGyY8 HTTP/1.1

HTTP/1.1 302 Found Location: http://localhost:59735/signin-facebook?code=longlongcode#=

GET http://localhost:59735/signin-facebook?code=longlongcodeHTTP/1.1 Host: localhost:59735

HTTP/1.1 302 Found Location: /Account/ExternalLoginCallback Set-Cookie: .AspNet.Correlation.Facebook=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT Set-Cookie: .AspNet.ExternalCookie=somecodehere; path=/; HttpOnly

CONNECT graph.facebook.com:443 HTTP/1.1 Host: graph.facebook.com Connection: Keep-Alive HTTP/1.1 200 Connection Established

GET https://graph.facebook.com/v2.8/oauth/access_token?grant_type=authorization_code&code=somecodehere&redirect_uri=http%3A%2F%2Flocalhost%3A59735%2Fsignin-facebook&client_id=1111111111111111&client_secret=some_secret_here HTTP/1.1 HTTP/1.1 200 OK {"access_token":"access_token_here","token_type":"bearer","expires_in":5109049}

GET https://graph.facebook.com/v2.8/me?access_token=some_token_here&%26appsecret_proof=proof_here HTTP/1.1 Host: graph.facebook.com HTTP/1.1 200 OK

GET http://localhost:59735/Account/ExternalLoginCallback HTTP/1.1

So workflow should be fine. But when I do:

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

private IAuthenticationManager AuthenticationManager
        {
            get
            {
                return HttpContext.GetOwinContext().Authentication;
            }
        }

in my ExternalLoginCallback controller I get null. Why is that? Please share how you get login information from external provider.

dnovhorodov avatar Mar 30 '17 07:03 dnovhorodov

@Tratcher, I just noticed and wanted to let you know that the Fields attribute is read-only at the moment (3.1.0 RC1). People will need to continue to set the required UserInformationEndpoint instead.

embee8 avatar Mar 30 '17 10:03 embee8

@embee8 what's the problem? Fields is a list you add to, just like scope. Neither let you assign the whole collection.

Tratcher avatar Mar 30 '17 10:03 Tratcher