private bool _TryGetCachedSession() { if (!_settings.HasSessionInfo) { HasCachedSessionInfo = false; return(false); } // Properties are gotten from _settings. _facebookApi = new FacebookWebApi(ApplicationKey, SessionKey, UserId, SessionSecret); HasCachedSessionInfo = true; return(true); }
/// <summary>Get the URI to host in a WebBrowser to allow a Facebook user to log into this application.</summary> /// <param name="authenticationToken"></param> /// <returns></returns> public Uri GetLoginUri(string successUri, string deniedUri, IEnumerable <Permissions> requiredPermissions) { return(FacebookWebApi.GetLoginUri(ApplicationKey, successUri, deniedUri, requiredPermissions)); }
/// <summary> /// Start a new session by connecting to Facebook. /// </summary> /// <param name="authenticationToken">The authentication token to use.</param> public void InitiateNewSession(Uri sessionResponse) { string sessionPrefix = "session="; var badSessionInfoException = new ArgumentException("The session response does not contain connection information.", "sessionResponse"); Verify.IsNotNull(sessionResponse, "sessionResponse"); if (!string.IsNullOrEmpty(SessionKey)) { throw new InvalidOperationException("This object already has a session."); } string jsonSessionInfo = sessionResponse.Query; jsonSessionInfo = Utility.UrlDecode(jsonSessionInfo); int startIndex = jsonSessionInfo.IndexOf(sessionPrefix); if (-1 == startIndex) { throw badSessionInfoException; } jsonSessionInfo = jsonSessionInfo.Substring(startIndex + sessionPrefix.Length); if (jsonSessionInfo.Length == 0 || jsonSessionInfo[0] != '{') { throw badSessionInfoException; } int curlyCount = 1; for (int i = 1; i < jsonSessionInfo.Length; ++i) { if (jsonSessionInfo[i] == '{') { ++curlyCount; } if (jsonSessionInfo[i] == '}') { --curlyCount; } if (curlyCount == 0) { jsonSessionInfo = jsonSessionInfo.Substring(0, i + 1); break; } } if (curlyCount != 0) { throw badSessionInfoException; } var serializer = new JsonSerializer(typeof(object)); var sessionMap = (IDictionary <string, object>)serializer.Deserialize(jsonSessionInfo); object sessionKey; object userId; object secret; if (!sessionMap.TryGetValue("session_key", out sessionKey) || !sessionMap.TryGetValue("uid", out userId) || !sessionMap.TryGetValue("secret", out secret) || string.IsNullOrEmpty(sessionKey.ToString()) || string.IsNullOrEmpty(userId.ToString()) || string.IsNullOrEmpty(secret.ToString())) { throw badSessionInfoException; } _settings.SetSessionInfo(sessionKey.ToString(), secret.ToString(), new FacebookObjectId(userId.ToString())); _settings.Save(); _facebookApi = new FacebookWebApi(ApplicationKey, SessionKey, UserId, SessionSecret); HasCachedSessionInfo = true; }