Facebook OAuth 突然停止工作

Facebook OAuth stopped working suddenly

我昨天注意到我网站的 Facebook 登录已停止工作。

这在过去的 2 个月里一直运行良好,据我所知我没有改变任何东西。我已经在链接上尝试了所有可能的方法,例如:- 以及更多...

ASP.NET MVC5 OWIN Facebook authentication suddenly not working

我注意到 Stack Overflow Facebook 身份验证也已停止工作。

有没有其他人注意到这一点并找到任何解决方案?值得注意的是我正在使用 azure 应用程序服务来托管。但是我在使用localhost时也发现了这个问题。

我当前的设置是这样的...

在Startup.Auth.cs

var facebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions()
{
    AppId = "xxxxxxxxxxxxx",
    AppSecret = "xxxxxxxxxxxx"
};
facebookOptions.Scope.Add("email");
app.UseFacebookAuthentication(facebookOptions);

在下面的方法中,loginInfo每次都是null

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

我还从另一个 post 建议添加了一个会话 "WAKEUP",fb 验证之前失败过一次,这次解决了这个问题,但它又回来了。

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
    Session["WAKEUP"] = "NOW!";

    // Request a redirect to the external login provider
    return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
}

很多人从昨天开始就遇到了麻烦。这是因为 Facebook 放弃了对其 API 的 v2.2 的支持。出于某种原因,他们的系统仍然将不使用版本号的 auth 调用重定向到 2.2 API。快速修复是确保 API 版本随 API 调用一起发送。

从 v2.3 开始,Facebook 也开始返回 JSON 对象。因此,请确保在代码中也进行更改。

正如 RockSheep 所解释的那样。 Facebook 放弃了对 API v2.2 的支持。您需要更新您的 OWIN nuget 包。

您可以找到 the issue on github(来自 Katana 项目)。

确保在你的 nuget 管理器中激活 pre releases,这样你就可以将 nuget 包更新到版本 v3.1.0-rc1.但要注意:更新后,您需要仔细测试您的登录(也许您还有其他身份验证提供商,如 Microsoft 或 Google,您也应该测试它们)。

技术

Api 将版本号更改为 v2.8,API 中的 return 值现在采用 JSON 格式,不再在网址。 'old' OWIN 包无法处理此更改。

[Oauth Access Token] Format - The response format of https://www.facebook.com/v2.3/oauth/access_token returned when you exchange a code for an access_token now return valid JSON instead of being URL encoded. The new format of this response is {"access_token": {TOKEN}, "token_type":{TYPE}, "expires_in":{TIME}}. We made this update to be compliant with section 5.1 of RFC 6749.

Here you can find the code-changes GitHub 以获得更多信息和更好的理解。

这里是为使用 scribe java 的人提供的解决方案。

 public Token extract(String response)
  {
    Preconditions.checkEmptyString(response, "Response body is incorrect. Can't extract a token from an empty string");
    JSONObject obj = new JSONObject(response);
    return new Token(obj.get("access_token").toString(), EMPTY_SECRET, response);
  }

我遇到了同样的问题,在这里找到了解决方案Fix facebook oauth 2017

基本上,您需要扩展 HttpClientHandler 并解码 JSON 响应而不是主体

创建一个新的 class 并将提取器设置为 JSON。

import org.scribe.builder.api.DefaultApi20;
import org.scribe.extractors.AccessTokenExtractor;
import org.scribe.extractors.JsonTokenExtractor;
import org.scribe.model.OAuthConfig;

public class FaceFmApi extends DefaultApi20 {

    @Override
    public String getAccessTokenEndpoint()
    {
        return "https://graph.facebook.com/oauth/access_token";
    }

    @Override
    public AccessTokenExtractor getAccessTokenExtractor()
    {
        return new JsonTokenExtractor();
    }

    @Override
    public String getAuthorizationUrl(OAuthConfig config) {
        return null;
    }
}

并注入您新创建的 class,如下所示。然后 getAccessToken() 将按预期工作。

public OAuthService getService() {
    return new ServiceBuilder().provider(FaceFmApi.class)
        .apiKey(config.getApiKey()).apiSecret(config.getApiSecret())
        .callback(config.getCallback()).build();
}