Skip to content

Instantly share code, notes, and snippets.

@simonhdickson
Last active March 21, 2017 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save simonhdickson/5570946 to your computer and use it in GitHub Desktop.
Save simonhdickson/5570946 to your computer and use it in GitHub Desktop.
Using a facebook token to authenticate your web api
public class TokenValidationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
var token = actionContext.Request.Headers.GetValues("Token").First();
if (this.IsTokenValid(token))
{
base.OnActionExecuting(actionContext);
return;
}
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden)
{
Content = new StringContent("Unauthorized User")
};
}
public bool IsTokenValid(string token)
{
using (var client = new WebClient())
{
var result =
client.DownloadString(
string.Format(
"https://graph.facebook.com/debug_token?input_token={0}&access_token={1}",
token,
YourFacebookApiKey));
var facebookToken = JObject.Parse(result);
return (bool)facebookToken["data"]["is_valid"];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment