ToUnixTime() public static method

Converts a DateTime object to unix time.
public static ToUnixTime ( System.DateTime dateTime ) : double
dateTime System.DateTime The date time.
return double
Example #1
0
        /// <summary>
        /// Creates a Facebook session from a signed request.
        /// </summary>
        /// <param name="appSecret">
        /// The app secret.
        /// </param>
        /// <param name="signedRequest">
        /// The signed request.
        /// </param>
        /// <returns>
        /// The Facebook session.
        /// </returns>
        internal static FacebookSession Create(IFacebookApplication settings, FacebookSignedRequest signedRequest)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            if (signedRequest == null)
            {
                return(null);
            }

            var data = (IDictionary <string, object>)signedRequest.Data;

            if (data == null)
            {
                return(null);
            }

            if (!data.ContainsKey("code") && string.IsNullOrEmpty(signedRequest.AccessToken))
            {
                return(null);
            }

            var dictionary = new JsonObject
            {
                { "uid", signedRequest.UserId.ToString() }
            };

            if (!string.IsNullOrEmpty(signedRequest.AccessToken))
            {
                dictionary["access_token"] = signedRequest.AccessToken;
            }

            if (data.ContainsKey("code"))
            {
                foreach (var key in data.Keys)
                {
                    dictionary[key] = data[key];
                }
            }
            else
            {
                if (signedRequest.Expires == DateTime.MaxValue)
                {
                    dictionary["expires"] = 0;
                }
                else if (signedRequest.Expires != DateTime.MinValue)
                {
                    dictionary["expires"] = DateTimeConvertor.ToUnixTime(signedRequest.Expires);
                }

                if (settings != null && !string.IsNullOrEmpty(settings.AppSecret))
                {
                    dictionary["sig"] = GenerateSessionSignature(settings.AppSecret, dictionary);
                }
            }

            return(new FacebookSession(dictionary, settings));
        }
Example #2
0
        /// <summary>
        /// Creates a facebook session from a signed request.
        /// </summary>
        /// <param name="appSecret">
        /// The app secret.
        /// </param>
        /// <param name="signedRequest">
        /// The signed request.
        /// </param>
        /// <returns>
        /// The facebook session.
        /// </returns>
        internal static FacebookSession Create(string appSecret, FacebookSignedRequest signedRequest)
        {
            if (signedRequest == null || String.IsNullOrEmpty(signedRequest.AccessToken))
            {
                return(null);
            }

            var dictionary = new JsonObject
            {
                { "uid", signedRequest.UserId.ToString() },
                { "access_token", signedRequest.AccessToken }
            };

            if (signedRequest.Expires == DateTime.MaxValue)
            {
                dictionary["expires"] = 0;
            }
            else if (signedRequest.Expires != DateTime.MinValue)
            {
                dictionary["expires"] = DateTimeConvertor.ToUnixTime(signedRequest.Expires);
            }

            dictionary["sig"] = GenerateSessionSignature(appSecret, dictionary);

            return(new FacebookSession(dictionary));
        }
Example #3
0
        /// <summary>
        /// Parse the signed request.
        /// </summary>
        /// <param name="secret">
        /// The secret.
        /// </param>
        /// <param name="signedRequestValue">
        /// The signed request value.
        /// </param>
        /// <returns>
        /// Returns the signed request.
        /// </returns>
        public static FacebookSignedRequest Parse(string secret, string signedRequestValue)
        {
            Contract.Requires(!string.IsNullOrEmpty(secret));
            Contract.Requires(!String.IsNullOrEmpty(signedRequestValue));
            Contract.Requires(signedRequestValue.Contains("."), Properties.Resources.InvalidSignedRequest);

            var result = TryParse(secret, signedRequestValue, 0, DateTimeConvertor.ToUnixTime(DateTime.UtcNow), true);

            return(result == null ? null : new FacebookSignedRequest(result));
        }
Example #4
0
        /// <summary>
        /// Try parsing the signed request.
        /// </summary>
        /// <param name="secret">
        /// The secret.
        /// </param>
        /// <param name="signedRequestValue">
        /// The signed request value.
        /// </param>
        /// <param name="signedRequest">
        /// The signed request.
        /// </param>
        /// <returns>
        /// Returns true if parsing was successful otherwise false.
        /// </returns>
        public static bool TryParse(string secret, string signedRequestValue, out FacebookSignedRequest signedRequest)
        {
            if (string.IsNullOrEmpty(signedRequestValue))
            {
                throw new ArgumentNullException("signedRequestValue");
            }
            if (string.IsNullOrEmpty(secret))
            {
                throw new ArgumentNullException("secret");
            }

            var result = TryParse(secret, signedRequestValue, 0, DateTimeConvertor.ToUnixTime(DateTime.UtcNow), false);

            signedRequest = result == null ? null : new FacebookSignedRequest(result);
            return(result != null);
        }
Example #5
0
        /// <summary>
        /// Creates a facebook session from a signed request.
        /// </summary>
        /// <param name="appSecret">
        /// The app secret.
        /// </param>
        /// <param name="signedRequest">
        /// The signed request.
        /// </param>
        /// <returns>
        /// The facebook session.
        /// </returns>
        internal static FacebookSession Create(string appSecret, string code)
        {
            FacebookOAuthClient client = new FacebookOAuthClient(FacebookApplication.Current);

            client.RedirectUri = new Uri(FacebookApplication.Current.CanvasUrl);
            dynamic response = client.ExchangeCodeForAccessToken(code);

            string accessToken = response.access_token;
            double expires     = Convert.ToDouble(response.expires);

            var dictionary = new JsonObject
            {
                { "access_token", accessToken }
            };

            dictionary["expires"] = DateTimeConvertor.ToUnixTime(DateTime.Now.AddSeconds(expires));
            dictionary["sig"]     = GenerateSessionSignature(appSecret, dictionary);

            return(new FacebookSession(dictionary));
        }
Example #6
0
        /// <summary>
        /// Parse the signed request.
        /// </summary>
        /// <param name="secret">
        /// The secret.
        /// </param>
        /// <param name="signedRequestValue">
        /// The signed request value.
        /// </param>
        /// <returns>
        /// Returns the signed request.
        /// </returns>
        public static FacebookSignedRequest Parse(string secret, string signedRequestValue)
        {
            var result = TryParse(secret, signedRequestValue, 0, DateTimeConvertor.ToUnixTime(DateTime.UtcNow), true);

            return(result == null ? null : new FacebookSignedRequest(result));
        }