Giter Site home page Giter Site logo

Comments (3)

prabirshrestha avatar prabirshrestha commented on July 24, 2024

I have added a new private class called FacebookWebHelper. Currently it is around 200 lines of code compared to thousand of line of Facebook.Web.dll and Facebook.Web.Mvc.dll.

Currently it is capable of parsing signed_request (both the oauth 2.0 signed_request cookie set by fb js sdk and the signed_request form value from canvas.)
There are two methods for parsing signed_request.
https://github.com/microsoft-dpe/facebook-csharp-sdk/commit/083ece31b81da663f4570c0331eb0ef696d6de50#L3R24
First method parses the signed_request string value.
https://github.com/microsoft-dpe/facebook-csharp-sdk/commit/083ece31b81da663f4570c0331eb0ef696d6de50#L3R66
Second method parses the signed_request value by looking at request.Forms["signed_request"](required by canvas app) and then request.Cookies["fbsr_appid"](required by fb js sdk). This method also caches the decoded signed_request per request, thus you will get performance benefits.

One huge advantage of FacebookWebHelper is it does not take any dependency on System.Web but rather uses Func<> so other projects like Nancy can easily consume it.

 public static object TryParseFacebookSignedRequest(string appId, string appSecret, Func<bool> isInCache, Func<object> getFromCache, Func<string> getSignedRequestFormValue, Func<string, string> getSignedRequestCookieValue, Action<object> cache, Func<string, object> deserializeObject, bool throws)

Here is an example how Nancy could make use of FacebookWebHelper. (We can easily support System.Web by using HttpContext instead of NancyContext with some other minor changes.)

    private static object TryParseFacebookSignedRequestInternal(NancyContext context, string appId, string appSecret, bool throws)
    {
        if (context == null)
            throw new ArgumentNullException("context");

        var request = context.Request;
        var items = context.Items;

        return FacebookWebHelper.TryParseFacebookSignedRequest(
            appId, appSecret,
            () => items.ContainsKey(SignedRequestKey),
            () => items[SignedRequestKey] as IDictionary<string, object>,
            () => request.Form.signed_request.HasValue ? request.Form.signed_request : string.Empty,
            cookieName => request.Cookies.ContainsKey(cookieName) ? request.Cookies[cookieName] : string.Empty,
            signedRequest => items[SignedRequestKey] = signedRequest,
            DeserializeObject,
            throws);
    }

There is also FacebookCanvasPageUrl method which is capable of generating url for facebook canvas application.
https://github.com/microsoft-dpe/facebook-csharp-sdk/commit/083ece31b81da663f4570c0331eb0ef696d6de50#L3R101

Here is a sample for FacebookCanvasPageUrl in Nancy. this can again be easily ported to System.Web.dll

    public static string FacebookCanvasPageUrl(this NancyContext context, string canvasPageOrAppName,
        bool? https = null, bool? beta = null)
    {
        if (context == null)
            throw new ArgumentNullException("context");
        if (context.Request == null)
            throw new ArgumentException("context.Request is null");

        var request = context.Request;

        return FacebookWebHelper.FacebookCanvasPageUrl(canvasPageOrAppName,
            () => request.Url.Scheme,
            () => request.Headers != null && !string.IsNullOrEmpty(request.Headers.Referrer) && new Uri(request.Headers.Referrer).Host == "apps.beta.facebook.com",
            https,
            beta);
    }

we can now call the method in the following manner: (assume I have added FacebookCanvasPageUrl as an extension method for HttpContextBase in for mvc)

 var url = HttpContext.FacebookCanvasPageUrl("appname"); // https://apps.facebook.com/appname

 // assume FacebookApplication.Current.CanvasPage = http://apps.facebook.com/appname
 url = HttpContext.FacebookCanvasePageUrl(FacebookApplication.Current); // https://apps.facebok.com/appname

 url = HttpContext.FacebookCanvasPageUrl("appname", https: false, beta: true); // http://apps.beta.facebook.com/appname

You can either pass only the appname or the full canvas page url from IFacebookApplication. You can also explicitly set https or beta. If it is null, it will look at the request and try to figure it out by itself.

Though FacebookWebHelper is small, it has huge advantage and can accomplish quite a lot.

I will be adding FacebookTabUrl() and FacebookPageUrl() in the future.

from facebook-csharp-sdk.

prabirshrestha avatar prabirshrestha commented on July 24, 2024

source is still there but the project references from the solution has been removed.

from facebook-csharp-sdk.

prabirshrestha avatar prabirshrestha commented on July 24, 2024

removing in v6.

will instead provide samples

from facebook-csharp-sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.