使用facebook SDK登录MVC5应用程序时获取错误(error = access_denied)

时间:2015-11-04 19:43:15

标签: asp.net-mvc facebook oauth-2.0

我用ASP.NET MVC5开发了一个应用程序。我在我的应用程序中使用了Facebook外部身份验证。 当我使用" Locallhost"调试此应用程序时域名,Facebook登录运行良好但是当我在主服务器上发布应用程序时,AuthenticationManager.GetExternalLoginInfo()返回null,它在url中给出了这样的错误:

http://xxxxx.com/Account/ExternalLoginCallback?ReturnUrl=%2Fen&error=access_denied#_=_

我已经设置了"网站网址" as" http://xxxx.com"和"有效的OAuth重定向URI" as" http://xxxx.com/signin-facebook"在Facebook开发控制台中。

我在Startup.Outh.cs文件中的设置是:

var FacebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions();
        FacebookOptions.AppId = ConfigurationManager.AppSettings["Facebook_User_Key"];
        FacebookOptions.AppSecret = ConfigurationManager.AppSettings["Facebook_Secret_Key"];
        FacebookOptions.Provider = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationProvider()
        {
            OnAuthenticated = async context =>
            {
                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);
                    string claimValue = claim.Value.ToString();
                    if (!context.Identity.HasClaim(claimType, claimValue))
                        context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Facebook"));
                }

            }
        };
        FacebookOptions.SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie;
        app.UseFacebookAuthentication(FacebookOptions);

我不知道为什么外部登录不能仅在具有我的主域名的服务器中工作。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

我遇到了你描述的几乎相同的症状:

<强>不久:

Facebook身份验证在localhost上运行良好,在将项目上传到另一台服务器(并更改Facebook控制台上的站点URL)后,身份验证未成功。

我建议您回滚到MVC模板代码,如果可以的话 - 请注意您对中间件代码所做的任何更改(Startup.Auth.sc)。 特别要注意与LOCAL配置交互的代码,例如本地服务的磁盘I / O和操作系统权限。

我的具体案例:

从Owin / Katana支持的WebAPI项目的Visual Studio模板开始,当在localhost上进行测试时,外部登录与Facebook,Microsoft和Google OAuth中间件完美配合。

后来我将代码添加到Startup.Auth.sc,因为我需要进一步的身份验证活动。

所以这是原始代码:

public void ConfigureAuth(IAppBuilder app)
    {
        // see WebAPI template of Visual Studio 2013/2015
        ...
        app.UseFacebookAuthentication(
            appId: 99999999,
            appSecret: *******);
    }

这是替代品:

public void ConfigureAuth(IAppBuilder app)
    {
        // see WebAPI template of Visual Studio 2013/2015
        ...
        app.UseFacebookAuthentication(GetFacebookAuth());            
    }
    private FacebookAuthenticationOptions GetFacebookAuth()
    {
        string picRequest =
            String.Format("/me/picture?redirect=false&width={0}&height={0}", ProfileInfoClaimsModel.PIC_SIDE_PX);
        var facebookProvider = new FacebookAuthenticationProvider()
        {
            OnAuthenticated = async (context) =>
            {
                var client = new FacebookClient(context.AccessToken);
                dynamic me = client.Get("/me?fields=id,name,locale");
                dynamic mePicture = client.Get(picRequest);

                // storing temporary social profile info TO A LOCAL FOLDER
                // uploading the local folder to a service WITH A LOCAL CREDENTIAL FILE
                ...
            }

        };
        var options = new FacebookAuthenticationOptions()
        {
            AppId = 0123456789,
            AppSecret = ******,
            Provider = facebookProvider,
        };
        return options;
    }

您可能会注意到我的评论会使问题变得明显 - 代码指向本地资源。

然后我将项目发布到运行带有IIS 8.5的Windows Server 2012的虚拟服务器(由Amazon EC2提供)。 从那一刻起,我一直在/ signin-facebook的重定向中获得error=access_denied

我决定遵循this好的旧概念,然后回到原来的模板代码。很快我发现我忘了配置新服务器了。例如,代码引用的文件夹不存在,并且该站点无权创建它。

显然,这解决了它。