Getting Up in Runing with FaceSharp is very simple
Just follow the steps below and you'll be ready to start coding- Create a new .NET MVC Project (3/4) or open an existing project.
- Add FaceSharp through NuGet
- Add the FacebookCore.cs file to your project (see below for sample)
- Add entry into Web.Config for Castle
- Add Attributes to your controllers
- Enjoy Facebook development with FaceSharp
FacebookCore.cs (created in Code/Facebook)
Be sure to replace <<your assembly>> with your web project's assembly name.using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Web; using FaceSharp.Api; using FaceSharp.Api.Types; namespace <<your assembly>>.Code.Facebook { public class FacebookCore : IFacebookCore { private readonly string _applicationId; private readonly string _apiKey; private readonly string _applicationSecret; private readonly string _applicationUrl; private readonly string _extendedPermissions; public string AccessToken { get { return HttpContext.Current.Items["access_token"] == null ? "" : HttpContext.Current.Items["access_token"].ToString(); } set { if (HttpContext.Current.Items["access_token"] == null) { HttpContext.Current.Items.Add("access_token", value); } else { HttpContext.Current.Items["access_token"] = value; } } } public string ApplicationId { get { return _applicationId; } } public string ApiKey { get { return _apiKey; } } public string ApplicationSecret { get { return _applicationSecret; } } public string ApplicationUrl { get { return _applicationUrl; } } public string ExtendedPermissions { get { return _extendedPermissions; } } public long CurrentUserId { get { long uid = 0; if (HttpContext.Current.Items["uid"] != null && !long.TryParse(HttpContext.Current.Items["uid"].ToString(), out uid)) { } return uid; } set { if (HttpContext.Current.Items["uid"] == null) { HttpContext.Current.Items.Add("uid", value); } else { HttpContext.Current.Items["uid"] = value; } } } public long ProfileId { get { long profileId = 0; if (HttpContext.Current.Items["profile_id"] != null && !long.TryParse(HttpContext.Current.Items["profile_id"].ToString(), out profileId)) { } return profileId; } set { if (HttpContext.Current.Items["profile_id"] == null) { HttpContext.Current.Items.Add("profile_id", value); } else { HttpContext.Current.Items["profile_id"] = value; } } } public FaceSharp.Api.Types.User CurrentUser { get { return (FaceSharp.Api.Types.User)HttpContext.Current.Items["currentUser"]; } set { if (HttpContext.Current.Items["currentUser"] == null) { HttpContext.Current.Items.Add("currentUser", value); } else { HttpContext.Current.Items["currentUser"] = value; } } } public void UserAuthenticated() { // This is a callback when users authenticate your app, you can use this to create a user in your database if needed } public FacebookCore() { _applicationId = ConfigurationManager.AppSettings["ApplicationId"]; _apiKey = ConfigurationManager.AppSettings["ApiKey"]; _applicationSecret = ConfigurationManager.AppSettings["ApplicationSecret"]; _applicationUrl = string.Empty; _extendedPermissions = ConfigurationManager.AppSettings["ExtendedPermissions"]; if (_applicationId != null && HttpContext.Current.Items["ApplicationId"] == null) HttpContext.Current.Items.Add("FacebookApplicationId", _applicationId); if (_apiKey != null && HttpContext.Current.Items["ApiKey"] == null) HttpContext.Current.Items.Add("ApiKey", _apiKey); if (_applicationSecret != null && HttpContext.Current.Items["ApplicationSecret"] == null) HttpContext.Current.Items.Add("FacebookApplicationSecret", _applicationSecret); // if (_applicationUrl != null && HttpContext.Current.Items["ApplicationUrl"] == null) HttpContext.Current.Items.Add("FacebookApplicationUrl", _applicationUrl); if (_extendedPermissions != null && HttpContext.Current.Items["ExtendedPermissions"] == null) HttpContext.Current.Items.Add("FacebookExtendedPermissions", _extendedPermissions); } } }
Web.config Settings
Be sure to replace <<your assembly>> with your web project's assembly name.Be sure to replace the Facebook Settings from the Facebook App you created.
<configSections> ... <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" /> ... </configSections> <castle> <components> <component id="FacebookCore" service="FaceSharp.Api.IFacebookCore,FaceSharp" type="<<your assembly>>.Code.Facebook.FacebookCore, <<your assembly>>" /> </components> </castle> <appSettings> .... <add key="FacebookApplicationId" value="<<Your Facebook App Id>>" /> <add key="FacebookApplicationSecret" value="<<Your Facebook App Secret>>" /> <add key="FacebookExtendedPermissions" value="<<Any Extended Permissions You Need>>" /> .... </appSettings>