Last active
March 21, 2017 16:43
-
-
Save simonhdickson/5570946 to your computer and use it in GitHub Desktop.
Using a facebook token to authenticate your web api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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