ASP.NET MVC5 OWIN Facebook authentication suddenly not working

后端 未结 11 2062
孤街浪徒
孤街浪徒 2020-11-27 03:31

Update 2017!

The issue I had when I posted the original question has got nothing to do with the recent changes Facebook made when they forced everyo

相关标签:
11条回答
  • 2020-11-27 04:12

    Check you get an outside internet connection from your application. If not, fix your outside internet connection. My problem was I was using an EC2 AWS instance that suddenly stopped connecting to the internet. It took me a while to realize that was the problem.

    0 讨论(0)
  • 2020-11-27 04:13

    Noticed this problem yesterday. Facebook does not support Microsoft.Owin.Security.Facebook version 3.0.1 anymore. For me it worked to install version 3.1.0. To update to 3.1.0, run the command Install-Package Microsoft.Owin.Security.Facebook in Package Manager Console: https://www.nuget.org/packages/Microsoft.Owin.Security.Facebook

    0 讨论(0)
  • 2020-11-27 04:13

    The last Facebook upgrade was on 2015-02-09 (https://www.nuget.org/packages/Microsoft.AspNet.WebPages.OAuth/)

    The latest version of the API at that point was version 2.2. Version 2.2 expired on the 25th of March 2017, which is coincidentally when the problem started. (https://developers.facebook.com/docs/apps/changelog)

    I'm guessing Facebook probably automatically upgraded the API and now the MS OAUTH library is unable to parse the new response.

    tldr: The Microsoft WebPages OAuth library is outdated (for FB at least) and you'll probably have to find another solution

    0 讨论(0)
  • 2020-11-27 04:16

    Ok I've got a solution to the problem.

    This is the code I had previously in my Startup.Auth.cs file:

    var x = new FacebookAuthenticationOptions();
                //x.Scope.Add("email");
                x.AppId = "1442725269277224";
                x.AppSecret = "<secret>";
                x.Provider = new FacebookAuthenticationProvider()
                {
                    OnAuthenticated = async context =>
                    {
                            //Get the access token from FB and store it in the database and
                        //use FacebookC# SDK to get more information about the user
                        context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken",context.AccessToken));
                        context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:name", context.Name));
                        context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:email", context.Email));
                    }
                };
                x.SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie;
                app.UseFacebookAuthentication(x);
    

    Notice how the

    x.Scope.Add("email")
    

    line has been commented out, but still I'm query-ing for the e-mail later in the OnAuthenticated handler? Yup, that's right. For some reason this worked flawlessly for a few weeks.

    My solution was to simply uncomment the x.Scope.Add("email"); line to make sure that the scope=email variable was present in the initial request to Facebook.

    Now everything works like it did!

    I cannot understand why this worked before like it was. The only explanation I can come up with is that Facebook changed something on their end.

    0 讨论(0)
  • 2020-11-27 04:20

    I have been working on solution for three days. And I've just found it on github(https://github.com/aspnet/AspNetKatana/issues/38#issuecomment-290400987)

    var facebookOptions = new FacebookAuthenticationOptions()
    {
        AppId = "xxxxx",
        AppSecret = "xxxxx",
    };
    
    // Set requested scope
    facebookOptions.Scope.Add("email");
    facebookOptions.Scope.Add("public_profile");
    
    // Set requested fields
    facebookOptions.Fields.Add("email");
    facebookOptions.Fields.Add("first_name");
    facebookOptions.Fields.Add("last_name");
    
    facebookOptions.Provider = new FacebookAuthenticationProvider()
    {
        OnAuthenticated = (context) =>
            {
                // Attach the access token if you need it later on for calls on behalf of the user
                context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));
    
                foreach (var claim in context.User)
                {
                    //var claimType = string.Format("urn:facebook:{0}", claim.Key);
                    var claimType = string.Format("{0}", claim.Key);
                    string claimValue = claim.Value.ToString();
    
                        if (!context.Identity.HasClaim(claimType, claimValue))
                            context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Facebook"));
                }
    
                return Task.FromResult(0);
           }
    };
    
    app.UseFacebookAuthentication(facebookOptions);
    

    And to get values

    var info = await AuthenticationManager.GetExternalLoginInfoAsync();
    
    if (info != null)
    {
        var firstName = info.ExternalIdentity.Claims.First(c => c.Type == "first_name").Value;
        var lastName = info.ExternalIdentity.Claims.First(c => c.Type == "last_name").Value;
    }
    
    0 讨论(0)
提交回复
热议问题