Apply Attributes on Controllers
Development Guide Page 2 of 4- Get FaceSharp with NuGet
- Apply Attributes on Controllers for authentication.
- Initialize Facebook JavaScript SDK Properly Learn how to initialize the Javascript SDK AND Facesharp Server Side at the same time.
- Make Facebook API Calls with FaceSharp
Using Attributes to Aid with Authorization
Decorating your Controller methods is a simple means of enforcing users are logged in and have authenticated your app.
[FacebookGraphApiFilter]
public ActionResult Index()
{
var facebookApi = new FacebookApi();
var currentUser = facebookApi.User.GetCurrentUser();
return View(currentUser);
}
[FacebookGraphApiFilter]
public ActionResult DynamicExample()
{
var facebookApi = new FacebookApi();
dynamic friends = facebookApi.GraphApi.MakeGraphRequest("me", "friends", new[] {"id", "name", "first_name", "last_name"});
return View(friends);
}
Attributes on Classes
You can also place the FacebookGraphApiFilter on an entire class if all methods are to be only accessed by authorized Facebook Users
[FacebookGraphApiFilter]
public class TestController : BaseController
{
public ActionResult Index()
{
var facebookApi = new FacebookApi();
var currentUser = facebookApi.User.GetCurrentUser();
return View(currentUser);
}
public ActionResult DynamicExample()
{
var facebookApi = new FacebookApi();
dynamic friends = facebookApi.GraphApi.MakeGraphRequest("me", "friends", new[] {"id", "name", "first_name", "last_name"});
return View(friends);
}
The FacebookGraphApiFilter uses your implementation of IFacebookCore to read in Facebook configuration details, you can supply them directly:
[FacebookGraphApiFilter(ApplicationId = "12345",ApplicationSecret = "12314")]
public class TestController : BaseController
{
public ActionResult Index()
Recommendations
Canvas Application Settings
Canvas apps should have both CanvasRedirect = true and ForceLogin = true- ForceLogin, TRUE = ensures that users must be authenticated before this action will be executed
- CanvasRedirect TRUE = will emit a javascript fragment redirecting the user to the Facebook authorization dialog.
- ResponseType "code token" - helps to initialize both JavaScript SDK and Facesharp Server Side SDK at the same time.
[FacebookGraphApiFilter(CanvasRedirect = true, ForceLogin = true, ResponseType = "code token")]
public ActionResult Index()
{
var facebookApi = new FacebookApi();
var currentUser = facebookApi.User.GetCurrentUser();
return View(currentUser);
}