Exemple #1
0
        internal async Task <AccessTokenData> LoginAsync(string permissions, bool force, FacebookLoginBehavior loginBehavior)
        {
            if (this.LoginInProgress)
            {
                throw new InvalidOperationException("Login in progress.");
            }

            this.LoginInProgress = true;
            try
            {
                var session = AccessTokenDataCacheProvider.Current.GetSessionData();
                if (session == null)
                {
#if WINDOWS
                    // Authenticate
                    var authResult = await PromptOAuthDialog(permissions, WebAuthenticationOptions.None, loginBehavior);
#else
                    var authResult = await PromptOAuthDialog(permissions, WebAuthenticationOptions.None, loginBehavior);
#endif

                    FacebookClient client     = new FacebookClient(authResult.AccessToken);
                    var            parameters = new Dictionary <string, object>();
                    parameters["fields"] = "id";

                    var result = await client.GetTaskAsync("me", parameters);

                    var dict = (IDictionary <string, object>)result;

                    session = new AccessTokenData
                    {
                        AccessToken = authResult.AccessToken,
                        Expires     = authResult.Expires,
                        FacebookId  = (string)dict["id"],
                    };
                }
                else
                {
                    // Check if we are requesting new permissions
                    bool newPermissions = false;
                    if (!string.IsNullOrEmpty(permissions))
                    {
                        var p = permissions.Split(',');
                        newPermissions = session.CurrentPermissions.Join(p, s1 => s1, s2 => s2, (s1, s2) => s1).Count() != p.Length;
                    }

                    // Prompt OAuth dialog if force renew is true or
                    // if new permissions are requested or
                    // if the access token is expired.
                    if (force || newPermissions || session.Expires <= DateTime.UtcNow)
                    {
#if WINDOWS
                        // Authenticate
                        var authResult = await PromptOAuthDialog(permissions, WebAuthenticationOptions.None, loginBehavior);
#else
                        var authResult = await PromptOAuthDialog(permissions, WebAuthenticationOptions.None, loginBehavior);
#endif
                        if (authResult != null)
                        {
                            session.AccessToken = authResult.AccessToken;
                            session.Expires     = authResult.Expires;
                        }
                    }
                }

                // Set the current known permissions
                if (!string.IsNullOrEmpty(permissions))
                {
                    var p = permissions.Split(',');
                    session.CurrentPermissions = session.CurrentPermissions.Union(p).ToList();
                }

                // Save session data
                AccessTokenDataCacheProvider.Current.SaveSessionData(session);
                CurrentAccessTokenData = session;
            }
            finally
            {
                this.LoginInProgress = false;
            }

            return(CurrentAccessTokenData);
        }