Beispiel #1
0
        /// <summary>
        /// Makes a request to the Salesforce client, wrapping the specified logic with the code necessary to handle authentication.
        /// </summary>
        /// <typeparam name="T">The type of value returned from the request.</typeparam>
        /// <param name="request">The request to make on the Salesforce client.</param>
        /// <exception cref="InvalidOperationException">The current user is not authenticated.</exception>
        /// <returns>The value returned by the request.</returns>
        public static async Task <T> MakeAuthenticatedClientRequestAsync <T>(Func <ForceClient, Task <T> > request)
        {
            bool done           = false;
            bool refreshedToken = false;
            T    result         = default(T);

            do
            {
                // 1. Get the token from the cache
                SalesforceToken salesforceToken = HttpContext.Current.Session[SalesforceService.TokenCacheKey] as SalesforceToken;

                // 2. If no token is available, redirect to authorize
                if (salesforceToken == null)
                {
                    // This exception message should be used to trigger the sign-in UI
                    throw new InvalidOperationException("AuthorizationRequired");
                }

                // Initialize the ForceClient
                ForceClient forceClient = salesforceToken.GetForceClient();

                try
                {
                    // 3. Invoke the request with the acquired token
                    result = await request(forceClient);

                    done = true;
                }
                catch (ForceException e)
                {
                    // If message is "Session expired or invalid", the access token is invalid, so eat the
                    // exception and fall through to try to update it using the refresh token.
                    if (e.Message != "Session expired or invalid")
                    {
                        throw;
                    }

                    if (refreshedToken)
                    {
                        // The access token is invalid and was already refreshed once, give up and
                        // re-throw the exception.
                        throw;
                    }
                }

                // 4. If the token is invalid, attempt to acquire a new access token from the refresh token
                if (!done)
                {
                    await SalesforceService.AcquireAccessTokenFromRefreshTokenAsync(salesforceToken);

                    refreshedToken = true;
                }
            } while (!done);

            return(result);
        }