protected void Page_Load(object sender, EventArgs e) { try { RestoreParameters(); if (RequestHelper.IsPostBack() && Page.Request.Params["__EVENTTARGET"] == "SaveButton") { // The access to the Settings page was lost, it might happen as the Internet Explorer opens the target URL of SalesForce redirection in a new window. if (CurrentUser.IsGlobalAdministrator) { // The current user is a global administrator, so it is safe to proceed. // Without this check there is a security vulnerability as any CMS Desk user could choose a site and authorize access to his or her Sales Cloud organization. string name = "CMSSalesForceCredentials"; if (!String.IsNullOrEmpty(SourceSiteName)) { name = String.Format("{0}.{1}", SourceSiteName, name); } CloseDialog(GetString("sf.authorization.partialsuccess")); SettingsKeyProvider.SetValue(name, CredentialsHiddenField.Value); CredentialsHiddenField.Value = String.Empty; } } else { RedirectUrlLiteral.Text = HTMLHelper.HTMLEncode(RedirectUrl); string authorizationCode = QueryHelper.GetString("code", null); if (!String.IsNullOrEmpty(authorizationCode)) { SalesForceAuthorizationHelper authorizationHelper = new SalesForceAuthorizationHelper(Credentials.ClientId, Credentials.ClientSecret, RedirectUrl); GetAuthenticationTokensResponse response = authorizationHelper.GetAuthenticationTokens(authorizationCode); Identity identity = authorizationHelper.GetIdentity(response); Credentials.RefreshToken = response.RefreshToken; Credentials.OrganizationBaseUrl = response.InstanceBaseUrl; Credentials.UserName = identity.UserName; Credentials.OrganizationName = GetOrganizationName(Credentials, identity.OrganizationId); StoreParameters(); if (URLHelper.Url.Scheme != SourceUrlScheme) { RedirectToScheme(SourceUrlScheme, SourceUrlPort); } else { CloseDialog(GetString("sf.authorization.success")); } } else { string state = QueryHelper.GetString("state", null); if (!String.IsNullOrEmpty(state)) { CloseDialog(GetString("sf.authorization.success")); } } } } catch (Exception exception) { HandleError(exception); } }
protected void ConfirmButton_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(ClientIdentifierTextBox.Text) || String.IsNullOrEmpty(ClientSecretTextBox.Text)) { SalesForceError.Report(GetString("sf.authorization.missingconsumerinput")); } else { Credentials.ClientId = ClientIdentifierTextBox.Text; Credentials.ClientSecret = ClientSecretTextBox.Text; try { StoreParameters(); SalesForceAuthorizationHelper authorizationHelper = new SalesForceAuthorizationHelper(Credentials.ClientId, Credentials.ClientSecret, RedirectUrl); string authorizationUrl = authorizationHelper.GetAuthorizationUrl(ParametersId); Response.Redirect(authorizationUrl, false); } catch (Exception exception) { HandleError(exception); } } }
protected void Page_Load(object sender, EventArgs e) { try { RestoreParameters(); if (RequestHelper.IsPostBack() && Page.Request.Params[postEventSourceID] == "SaveButton") { // The access to the Settings page was lost, it might happen as the Internet Explorer opens the target URL of SalesForce redirection in a new window. if (CurrentUser.IsGlobalAdministrator) { // The current user is a global administrator, so it is safe to proceed. // Without this check there is a security vulnerability as any CMS Desk user could choose a site and authorize access to his or her Sales Cloud organization. CloseDialog(GetString("sf.authorization.partialsuccess")); SettingsKeyInfoProvider.SetValue("CMSSalesForceCredentials", SourceSiteName, CredentialsHiddenField.Value); CredentialsHiddenField.Value = String.Empty; } } else { RedirectUrlLiteral.Text = HTMLHelper.HTMLEncode(RedirectUrl); string authorizationCode = QueryHelper.GetString("code", null); if (!String.IsNullOrEmpty(authorizationCode)) { SalesForceAuthorizationHelper authorizationHelper = new SalesForceAuthorizationHelper(Credentials.ClientId, Credentials.ClientSecret, RedirectUrl); GetAuthenticationTokensResponse response = authorizationHelper.GetAuthenticationTokens(authorizationCode); Identity identity = authorizationHelper.GetIdentity(response); Credentials.RefreshToken = response.RefreshToken; Credentials.OrganizationBaseUrl = response.InstanceBaseUrl; Credentials.UserName = identity.UserName; Credentials.OrganizationName = GetOrganizationName(Credentials, identity.OrganizationId); StoreParameters(); if (RequestContext.CurrentScheme != SourceUrlScheme) { RedirectToScheme(SourceUrlScheme, SourceUrlPort); } else { CloseDialog(GetString("sf.authorization.success")); } } else { string state = QueryHelper.GetString("state", null); if (!String.IsNullOrEmpty(state)) { CloseDialog(GetString("sf.authorization.success")); } } } } catch (Exception exception) { HandleError(exception); } }