我浏览过关于Facebook取消授权网址的所有帖子,但没有一个解决我的问题。我有一个ASP.Net Web Api 2端点,但我无法正确通过ping。这是我的Route签名的样子:
[AllowAnonymous, Route("FacebookDeauthorize"), HttpPost]
public async Task<IHttpActionResult> FacebookDeauthorize(string signed_request)
{
//code for reading it
}
这导致404,所以我尝试将类型更改为对象并获取类型,以便我可以弄清楚它是通过什么传递的。当我尝试访问该对象时,它抛出了一个空引用异常,所以我认为它根本就没有通过。由于这是Web API,我无法查看表单请求以获取签名请求。有没有人成功地使用Web Api工作?关于路由签名应该是什么的任何帮助/指针?
答案 0 :(得分:1)
对于任何发现这一点的人来说,这里是asp.net web api和Facebook c#sdk的工作端点,技巧是使用模型而不是原语:
public class FacebookDeauthModel
{
public string signed_request { get; set; }
}
[AllowAnonymous]
[Route("FacebookDeauthorize")]
[HttpPost]
public async Task<IHttpActionResult> FacebookDeauthorize(FacebookDeauthModel model)
{
FacebookClient fb = new FacebookClient();
dynamic signedRequest = JsonConvert.DeserializeObject(fb.ParseSignedRequest("YOUR_APP_SECRET", model.signed_request).ToString());
string FBUserID = signedRequest.user_id;
ApplicationUser user = UserManager.FindBy(x => x.FBAppID == FBUserID);
if (user != null)
{
user.IsActive = false;
user.InactiveReason = "Facebook deauthorized on " + DateTime.UtcNow;
await UserManager.UpdateAsync(user);
}
else
{
_tracer.Error(Request, "FacebookDeauthorize", "Facebook tried to deauthorize a user we do not have record of, FBAppID: {0}", FBUserID);
}
return Ok();
}