/// <summary> /// Gets a Salesforce token based on the refresh token and adds it to the cache. /// </summary> /// <param name="salesforceToken">The Salesforce token that contains the refresh token to use.</param> /// <returns>The asynchronous task.</returns> private static async Task AcquireAccessTokenFromRefreshTokenAsync(SalesforceToken salesforceToken) { // Remove the old token from the cache HttpContext.Current.Session[SalesforceService.TokenCacheKey] = null; try { using (AuthenticationClient authenticationClient = salesforceToken.GetAuthenticationClient()) { // Attempt to refresh the token await authenticationClient.TokenRefreshAsync( SalesforceService.GetAppSetting("Salesforce:ConsumerKey"), salesforceToken.RefreshToken, string.Empty, SalesforceService.GetAppSetting("Salesforce:Domain") + "/services/oauth2/token"); salesforceToken = new SalesforceToken(authenticationClient); // Add the new token to the cache HttpContext.Current.Session[SalesforceService.TokenCacheKey] = salesforceToken; } } catch (ForceException e) { // InvalidGrant means that the refresh token is invalid. // Just return in that case, so re-authorization can occur. if (e.Error != Error.InvalidGrant) { throw; } } }
/// <summary> /// Gets the Salesforce authorization URL. The optional target parameter allows the app to redirect to /// a specified page after authorization; if the parameter is not specified, the app redirects to the current /// request's URL. /// </summary> /// <returns>The Salesforce authorization URL.</returns> public static string GetAuthorizationUrl(string targetUri = null) { return(Common.FormatAuthUrl( SalesforceService.GetAppSetting("Salesforce:Domain") + "/services/oauth2/authorize", ResponseTypes.Code, SalesforceService.GetAppSetting("Salesforce:ConsumerKey"), HttpUtility.UrlEncode(SalesforceOAuthRedirectHandler.GetAbsoluteRedirectUri()), DisplayTypes.Page, false, HttpUtility.UrlEncode(string.IsNullOrEmpty(targetUri) ? HttpContext.Current.Request.Url.AbsoluteUri : targetUri))); }
/// <summary> /// Gets the absolute Uri to redirect to after Salesforce has completed authenticating a user. This is the Uri /// to this handler. /// </summary> /// <returns>The absolute redirect Uri.</returns> private static string GetAbsoluteRedirectUri() { Uri redirectUri; Uri.TryCreate(SalesforceService.GetAppSetting("Salesforce:RedirectUri"), UriKind.RelativeOrAbsolute, out redirectUri); if (redirectUri.IsAbsoluteUri) { return(redirectUri.ToString()); } else { string uriAuthority = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority); return(new Uri(new Uri(uriAuthority), redirectUri).ToString()); } }
/// <summary> /// Gets a Salesforce token from an authorization code and adds it to the cache. /// </summary> /// <param name="authorizationCode">The code that was returned to the OAuth callback.</param> /// <param name="redirectUri">The redirect URI that was used to acquire the code.</param> /// <returns>The asynchronous task.</returns> public static async Task AcquireTokenByAuthorizationCodeAsync(string authorizationCode, string redirectUri) { using (AuthenticationClient authenticationClient = new AuthenticationClient()) { await authenticationClient.WebServerAsync( SalesforceService.GetAppSetting("Salesforce:ConsumerKey"), SalesforceService.GetAppSetting("Salesforce:ConsumerSecret"), redirectUri, authorizationCode, SalesforceService.GetAppSetting("Salesforce:Domain") + "/services/oauth2/token"); SalesforceToken salesforceToken = new SalesforceToken(authenticationClient); // Add the new token to the cache HttpContext.Current.Session[SalesforceService.TokenCacheKey] = salesforceToken; } }