开发者

Best practice for dual-use iFrame + External authentication for Facebook enabled app

开发者 https://www.devze.com 2023-02-01 12:20 出处:网络
Okay, if cookies are a no-no, then I need a little guidance as to the best way to implement the application(s) that I\'m creating.

Okay, if cookies are a no-no, then I need a little guidance as to the best way to implement the application(s) that I'm creating.

The scenario is that I'm trying to create a single Asp.Net MVC application that can authenticate a user regardless of whether the user visits a site directly or via an iFrame in Facebook. There are separate actions (in separate controllers, actually) for getting INTO the app depending on whether the user enters via Facebook or not, but there are also places in the Facebook app where I'm opening up a new window to "extended" functionality in other areas of the application that can't really work well within the iFrame. It is supposed to transition seamlessly. It's currently working quite well using cookies, but I've from multiple sources that this is not a good thing for iFrame apps. However, I'm not sure exactly what this means.

Without cookies, can you still somehow get server-side access to the authentication token? If not, then what is the "right" way to handle this. Do I need to resort to manually parsing the token using the JS API and sending an AJAX notification to the server of the fact that the user is authenticated and create a forms auth token? Will the CanvasAuthorize attribute work without cookies? Right now I have added code to the FormsAuthentication_OnAuthenticate event in Global.asax to create the forms auth token if the user is logged in via Facebook (and properly associated with a valid user in the external app) as follows:

protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs args)
{
    if (FormsAuthentication.CookiesSupported)
    {
        if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
   开发者_开发知识库     {
            // Attempt to authenticate using Facebook
            try
            {
                FacebookApp fbApp = new FacebookApp();
                if (fbApp.Session != null)
                {
                    dynamic me = fbApp.Get("me");
                    String fbID = "" + me.id;
                    MembershipUser mUser = AppMembershipProvider.GetUserByFacebookID(fbID);
                    if (mUser != null)
                    {
                        FormsAuthentication.SetAuthCookie(mUser.UserName, false);
                        AppMembershipProvider.UpdateLastLogin(mUser.UserName);
                        Session["FacebookLogin"] = true;
                    }
                }
            }
            catch (Exception e)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(e);
            }
        }
    }
    else
    {
        throw new HttpException("Cookieless Forms Authentication is not " +
                                "supported for this application.");
    }
}

Will I need to change this?

Sorry if this is basic knowledge, but I'm confused as to how best to implement this. Thanks!


First, let me address the issue with the cookies. So, when I say to not use cookies in iFrames I am saying that for a couple reasons. First in IE, there are some security issues. You need to add the following header to your app to make cookies work correctly inside iframes:

P3P: CP="CAO PSA OUR"

The second big issue with cookies in iframe apps is Safari. Due to security settings in Safari, cookies cannot be created by iframes. As such, you will not be able to rely on cookies for authentication inside of iframes.

Give that you are using the app inside and outside of the iframe, you should have cookie support turned on. However, your app must be designed in a way that will work around the iframe issues. That is going to be the hard part.

The most reliable authentication inside iframe apps is the signed request method. What happens is facebook will append a query parameter to your url when the url is rendered inside the iframe. This query parameter contains the user's session. The Facebook C# SDK handles reading this for you, so you dont need to parse it etc. But you need to be aware that it is there. If you view the incoming request url of your iframe app in facebook you will see something like http://www.mysite.com/page/?signed_request={blahblahblah}.

So the key is that you need to make sure that if you are in the iframe you keep that ?signed_request value on the url.

You can do this several ways. First, you can use the CanvasRedirect methods. These are extension methods on System.Web.Mvc.Controller in the Facebook.Web.Mvc namespace. The canvas redirect uses javascript to redirect your page in the top url. This way Facebook is actually handling the redirects and will always add the signed_request to your iframe url. The problem for you is that this method of redirecting will only work in the iframe, not outside.

The second way would be to manually add the ?signed_request to the url when you redirect. You would do something like:

public ActionResult Something() { return RedirectToAction("something", new { signed_request = Request.Querystring["signed_requets"]); }

There are other ways also, like storing data in the session or something, but I wouldn't recommend going down that path.

What you are doing is definitely an advanced senario, but hopefully the above will help you get going in the right direction. Feel free to contact me directly if you have any questions. nathan@ntotten.com or @ntotten on twitter.


I am in a similar situation to you. What I do to handle the various situations that can arise is:

  • Enable cookies in both the C# and JavaScript SDK.
  • Create a custom actionfilter that inherits from FacebookAuthorizeAttribute and overrides the HandleUnauthorizedRequest method to redirect to either a connect authorization page or an action decorated with the CanvasAuthorizeAttribute.
  • Pass either the signed_request (canvas app) or auth_token (connect app) as a querystring parameter to everything.
  • Check for null sessions and oauth tokens that don't match what has been passed in the querystring.

The main point is to ensure that both the session and oauth tokens are valid. When inside Facebook the signed_request will ensure this is true. By passing the token from your connect auth page you can ensure you have a valid token to inject into the FacebookApp constructor.

    public class FbAuthenticateAttribute : FacebookAuthorizeAttribute
    {
        private FacebookApp _fbApp;

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            var accessToken = filterContext.HttpContext.Request.Params["access_token"];
            if (FacebookApp.AccessToken != accessToken && !string.IsNullOrEmpty(accessToken))
            {
                _fbApp = new FacebookApp(accessToken);
            }
            else
            {
                _fbApp = FacebookApp;
            }
            filterContext.Controller.ViewBag.Context = GetContext().ToString();
            filterContext.RequestContext.HttpContext.Response.AppendHeader("p3p", "CP=\"CAO PSA OUR\"");
            try
            {
                dynamic user = _fbApp.Get("me");
                var signedRequest = filterContext.HttpContext.Request.Params["signed_request"];
                filterContext.Controller.ViewBag.QueryString = string.IsNullOrEmpty(signedRequest)
                                          ? "?access_token=" + _fbApp.AccessToken
                                          : "?signed_request=" + signedRequest;
            }
            catch (Exception ex)
            {
                string url = GetRedirectUrl(filterContext);
                filterContext.Result = new RedirectResult(url);
            }
        }

        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            string url = GetRedirectUrl(filterContext);
            filterContext.Result = new RedirectResult(url);
        }

        private string GetRedirectUrl(ControllerContext filterContext)
        {
            return new UrlHelper(filterContext.RequestContext).Action(GetRedirectAction(GetContext()), "Authentication");
        }

        private Enums.AppContext GetContext()
        {
            //Note: can't rely on this alone - find something more robust
            return FacebookApp.SignedRequest == null ? Enums.AppContext.FBWeb : Enums.AppContext.FBApp;
        }

        private string GetRedirectAction(Enums.AppContext context)
        {
            return context == Enums.AppContext.FBWeb ? "ConnectAuthenticate" : "Authenticate";
        }
    }

It could definitely do with a refactor and still has problems but is the best solution I have found so far.

0

精彩评论

暂无评论...
验证码 换一张
取 消