Example #1
0
        /// <summary>
        /// Logs in current gateway instance.
        /// </summary>
        public LoginResult Login(string code, out string refreshToken)
        {
            RestClient client = null;
            {
                client = new RestClient(ConfigurationManager.AppSettings["SalesforceBaseUrl"]);
            }

            var request = new RestRequest(Method.POST)
            {
                Resource = ConfigurationManager.AppSettings["SalesforceTokenResource"], RequestFormat = DataFormat.Json
            };

            IdentityServiceResponse identityResponse = null; TokenResponse response = null;

            request.AddParameter("grant_type", FLOW_AUTHORIZATION_CODE);

            request.AddParameter("redirect_uri", ConfigurationManager.AppSettings["SalesforceRedirectUrl"]);
            request.AddParameter("client_id", ConfigurationManager.AppSettings["SalesforceClientId"]);
            request.AddParameter("client_secret", ConfigurationManager.AppSettings["SalesforceClientSecret"]);

            request.AddParameter("code", code);

            if ((response = client.Execute <TokenResponse>(request).Data) == null)
            {
                throw new Exception(String.Format("Salesforce token obtain failed, authorization code: {0}", code));
            }

            var bearer = new OAuth2AuthorizationRequestHeaderAuthenticator(response.AccessToken);

            {
                RestClient.BaseUrl = new Uri(response.InstanceUrl); RestClient.Authenticator = bearer;
            }

            client = new RestClient(response.Id); client.Authenticator = bearer;

            var identityRequest = new RestRequest(Method.GET)
            {
                RequestFormat = DataFormat.Json
            };

            if ((identityResponse = client.Execute <IdentityServiceResponse>(identityRequest).Data) == null)
            {
                throw new InvalidOperationException(String.Format("Salesforce identity failed, id: {0}", response.Id));
            }

            var result = new LoginResult(identityResponse.UserId)
            {
                Token = response.AccessToken, InstanceUrl = response.InstanceUrl
            };

            refreshToken = response.RefreshToken;
            {
                return(result);
            }
        }
Example #2
0
        /// <summary>
        /// Logs in current gateway instance.
        /// </summary>
        public LoginResult Login(string username, string password, string token)
        {
            RestClient client = null;
            {
                client = new RestClient(ConfigurationManager.AppSettings["SalesforceBaseUrl"]);
            }

            var request = new RestRequest(Method.POST)
            {
                Resource = ConfigurationManager.AppSettings["SalesforceTokenResource"], RequestFormat = DataFormat.Json
            };

            IdentityServiceResponse identityResponse = null; TokenResponse response = null;

            request.AddParameter("grant_type", FLOW_PASSWORD);

            request.AddParameter("password", String.Format("{0}{1}", password, token));

            request.AddParameter("client_id", ConfigurationManager.AppSettings["SalesforceClientId"]);
            request.AddParameter("client_secret", ConfigurationManager.AppSettings["SalesforceClientSecret"]);

            request.AddParameter("username", username);

            if ((response = client.Execute <TokenResponse>(request).Data) == null)
            {
                throw new Exception(String.Format("Salesforce token obtain failed, username: {0}", username));
            }

            var bearer = new OAuth2AuthorizationRequestHeaderAuthenticator(response.AccessToken);

            {
                RestClient.BaseUrl = new Uri(response.InstanceUrl); RestClient.Authenticator = bearer;
            }

            client = new RestClient(response.Id); client.Authenticator = bearer;

            var identityRequest = new RestRequest(Method.GET)
            {
                RequestFormat = DataFormat.Json
            };

            if ((identityResponse = client.Execute <IdentityServiceResponse>(identityRequest).Data) == null)
            {
                throw new InvalidOperationException(String.Format("Salesforce identity failed, id: {0}", response.Id));
            }

            var result = new LoginResult(identityResponse.UserId)
            {
                Token = response.AccessToken, InstanceUrl = response.InstanceUrl
            };

            return(result);
        }