Ejemplo n.º 1
0
        /// <summary>
        /// Authorize
        ///
        /// Required member of the IAuthorizationService provider returns true/false to indicate
        /// if user has been authorized
        /// </summary>
        /// <param name="httpContect"></param>
        /// <returns></returns>
        ///
        public bool Authorize(HttpContextBase httpContext)
        {
            if (!String.IsNullOrEmpty(RaceDayConfiguration.Instance.DebugUser))
            {
                FormsAuthenticationTicket         ticket = CreateFormsTicket(RaceDayConfiguration.Instance.DebugUser, "", Int32.MaxValue);
                System.Web.Security.FormsIdentity id     = new System.Web.Security.FormsIdentity(ticket);

                FacebookUser fbUser = FacebookUser.Create(id, null);
                fbUser.id         = ticket.Name;
                fbUser.first_name = "Johnny";
                fbUser.last_name  = "Test";
                fbUser.email      = "*****@*****.**";
                httpContext.User  = fbUser;

                return(true);
            }

            if (!String.IsNullOrEmpty(httpContext.Request.QueryString["code"]))
            {
                String redirectUrl = String.Concat(httpContext.Request.Url.Scheme, "://", httpContext.Request.Url.Host, (!httpContext.Request.Url.IsDefaultPort ? ":" + httpContext.Request.Url.Port : ""), httpContext.Request.Path);

                FacebookConnection fbObject = new FacebookConnection();
                fbObject.GetFacebookAccessToken(redirectUrl, httpContext.Request.QueryString["code"]);
                fbObject.GetFacebookUserId();

                FormsAuthenticationTicket         ticket = CreateFormsTicket(fbObject.user_id, fbObject.access_token, fbObject.token_expires);
                System.Web.Security.FormsIdentity id     = new System.Web.Security.FormsIdentity(ticket);

                FacebookConnection fb = new FacebookConnection(id);
                httpContext.User = fb.GetFacebookUser(ticket.Name);

                httpContext.Response.Redirect(httpContext.Request.Path);
                return(true);
            }

            return(IsFacebookAuthorized(httpContext));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// GetFacebookUser
        ///
        /// Retrieves the specified user's information from the Facebook Graph API
        /// </summary>
        /// <returns></returns>
        ///
        public FacebookUser GetFacebookUser(String fbUserId)
        {
            if (String.IsNullOrEmpty(access_token))
            {
                throw new InvalidOperationException("Graph API access token not set");
            }

            String userUrl = String.Format(OBJECT_URL, fbUserId, access_token);

            userUrl += "&fields=id,name,first_name,last_name,email";
            String userResponse = GetHttpRequest(userUrl);

            if (!String.IsNullOrEmpty(userResponse))
            {
                String picUrl = String.Format(RELATION_URL, fbUserId, "picture", access_token);

                JObject jsonUser = JObject.Parse(userResponse);
                jsonUser.Add("picture", picUrl);

                return(FacebookUser.Create(identity, jsonUser));
            }

            return(null);
        }