예제 #1
0
    new protected void Page_Load(object sender, EventArgs e)
    {
        CanvasSession fs = new Facebook.Session.FBMLCanvasSession(
            ConfigurationSettings.AppSettings["APIKey"].ToString(),
            ConfigurationSettings.AppSettings["Secret"].ToString()
            );
        Api  api    = new Facebook.Rest.Api(fs);
        user fbUser = api.Users.GetInfo();      // Uygulamayi kullanan kullanicinin bilgileri

        UserFbId = fbUser.uid.ToString();
    }
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            FacebookSession session = null;
            FilterContext = filterContext;
            switch (PageType)
            {
                case FacebookPageType.Connect:
                    session = new ConnectSession(ApiKey ?? WebConfigurationManager.AppSettings["ApiKey"],
                        Secret ?? WebConfigurationManager.AppSettings["Secret"]);
                    break;
                case FacebookPageType.IFrame:
                    session = new IFrameCanvasSession(ApiKey ?? WebConfigurationManager.AppSettings["ApiKey"],
                        Secret ?? WebConfigurationManager.AppSettings["Secret"],
                        ParsePermissions(RequiredPermissions));
                    break;
                case FacebookPageType.FBML:
                    session = new FBMLCanvasSession(ApiKey ?? WebConfigurationManager.AppSettings["ApiKey"],
                        Secret ?? WebConfigurationManager.AppSettings["Secret"],
                        ParsePermissions(RequiredPermissions));
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            if (string.IsNullOrEmpty(session.SessionKey))
            {
                AddFormValuesToCookie(filterContext.HttpContext.Response, filterContext.HttpContext.Request.Form);
                if(session is CanvasSession)
                {
                    filterContext.Result = new ContentResult { Content = ((CanvasSession)session).GetRedirect() };
                }
                else
                {
                    var returnUrl = HttpUtility.UrlEncode(filterContext.HttpContext.Request.Url.ToString());
                    filterContext.Result = new RedirectResult("/account/logon/?returnUrl=" + returnUrl);
                }
                return;
            }

            var permissionsString = session.CheckPermissions();
            if (!string.IsNullOrEmpty(permissionsString))
            {
                AddFormValuesToCookie(filterContext.HttpContext.Response, filterContext.HttpContext.Request.Form);
                if (session is CanvasSession)
                {
                    filterContext.Result = new ContentResult
                   	{
                   		Content = ((CanvasSession) session).GetPermissionsRedirect(
                   			session.GetPermissionUrl(permissionsString, ((CanvasSession) session).GetNextUrl()))
                   	};
                }
                return;
            }

            // Everything is ok, so I need to stash this session somewhere for use.
            filterContext.HttpContext.Items.Add(FACEBOOK_CANVAS_SESSION, session);

            // Now I need to set the user context
            var currentUserId = session.UserId;
            if (currentUserId != 0)
            {
                var identity = new GenericIdentity(currentUserId.ToString());
                filterContext.HttpContext.User = new GenericPrincipal(identity, null);
            }
        }
 public static IFacebookApi GetApi(this Controller controller)
 {
     FBMLCanvasSession session = new FBMLCanvasSession(WebConfigurationManager.AppSettings["ApiKey"], WebConfigurationManager.AppSettings["Secret"]);
     return new Api().Initialize(session);
 }