我正在构建一个 ASP.NET (4.6) WebApi 项目,我正在使用 ASP.NET Identity 向 Facebook、Google 和 Microsoft 对我的 API 进行身份验证。我已成功通过 Google 和 Microsoft 的身份验证,但没有通过 Facebook 进行身份验证。 我正在使用 Visual Studio 2015
问题是我每次验证时都被拒绝访问。场景是这样的:
- 我通过浏览器对 localhost:2975/api/Account/ExternalLogins?returnUrl=%2F&generateState=true 进行 API 调用。
- 对于我的 API 支持的每个外部提供商,我都会收到指向我的 API 的链接。在 Facebook 案例中,我得到了 localhost:2975/api/Account/ExternalLogin?provider=Facebook&response_type=token&client_id=self&redirect_uri=http%3A%2F%2Flocalhost%3A2975%2F&state=99...01
- 我使用浏览器访问该链接,然后被重定向到 Facebook 登录页面。
- 我向 Facebook 进行身份验证,然后会出现一个对话窗口并请求获得有关我的请求信息的许可。
- 我接受并被重定向回我的 API,并显示错误“access_denied”。调用 localhost:2975/api/Account/ExternalLogin。
我还没有弄清楚问题出在哪里,是我的 Facebook 应用程序中的某些权限设置、我登录的 Facebook 用户,还是问题出在 ASP.NET Identity 模板中。
关于 Facebook,我已经为我的应用程序创建了一个测试应用程序,并将其作为实时应用程序推送。我可以在我的 Facebook 个人资料中找到该应用程序,而且我已经将其删除了好几次。我的 Facebook 个人资料具有应用程序的管理员角色。我创建了测试用户,并添加了我的一个 friend 作为应用程序的开发者/测试者。问题依然存在。我在某处读到应该有一个我应该接受的应用程序的待处理请求,但我没有找到任何请求。
关于 ASP.NET Identity 模板,在过程的早期收到错误:
账户 Controller .cs:
public async Task<IHttpActionResult> GetExternalLogin(string provider, string error = null)
{
if (error != null)
{
// This is where the API comes in step 5 above
return Redirect(Url.Content("~/") + "#error=" + Uri.EscapeDataString(error));
}
if (!User.Identity.IsAuthenticated)
{
// This is where the API comes in step 2 above before it redirects me to Facebook login
return new ChallengeResult(provider, this);
}
ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);
// Untouched logic
// ....
}
当我使用 Google Chromes 网络工具时,我可以看到我被重定向到此链接: facebook.com/login.php?skip_api_login=1&api_key=XXX&signed_next=1&next=https%3A%2F%2Fwww.facebook.com%2Fv2.6%2Fdialog%2Foauth%3Fredirect_uri%3Dhttp%253A%252F%252Flocalhost%253A2975%252Fsignin- facebook%26state%3D...%26scope%3Demail%252Cpublic_profile%26response_type%3Dcode%26client_id%3D...%26ret%3Dlogin%26logger_id%3D...&cancel_url=http%3A%2F%2Flocalhost%3A2975%2Fsignin- facebook%3Ferror%3Daccess_denied%26error_code%3D200%26error_description%3DPermissions%2Berror%26error_reason%3Duser_denied%26state%3D...%23_%3D_&display=page&locale=sv_SE&logger_id=... 有一个带有预定义错误的取消 url:“access_denied”,错误代码:200,原因:“user_denied”,所以我收到的“access_denied”错误可能不是很准确?
我能想出的其他相关代码在 Startup.Auth.cs 中(尽管我从未接触过 OnAuthenticated 部分):
var facebookOptions = new FacebookAuthenticationOptions()
{
AppId = "XXX",
AppSecret = "XXX",
Scope = { "email", "public_profile" }
};
facebookOptions.Provider = new FacebookAuthenticationProvider()
{
OnAuthenticated = (context) =>
{
context.Identity.AddClaim(new Claim("urn:facebook:access_token", context.AccessToken, ClaimValueTypes.String, "Facebook"));
context.Identity.AddClaim(new Claim("urn:facebook:email", context.Email, ClaimValueTypes.Email, "Facebook"));
return Task.FromResult(0);
}
};
app.UseFacebookAuthentication(facebookOptions);
感觉好像什么都试过了。关于如何解决这个问题的任何想法?
提前致谢!
最佳答案
更新 NuGet 包解决了我的问题。
关于facebook-graph-api - ASP.NET WebApi Identity Facebook 登录访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38075611/