protected void Page_Load(object sender, EventArgs e)
    {
        PageTitle.TitleText            = ResHelper.GetString("socialnetworking.linkedin.accesstoken");
        PageTitle.ShowFullScreenButton = false;
        PageTitle.ShowCloseButton      = false;

        string txtToken       = QueryHelper.GetString("txtToken", String.Empty);
        string consumerKey    = QueryHelper.GetString("apiKey", String.Empty);
        string consumerSecret = QueryHelper.GetString("apiSecret", String.Empty);
        string oauthToken     = QueryHelper.GetString("oauth_token", String.Empty);
        string error          = QueryHelper.GetString("oauth_problem", String.Empty);

        // Check Social networking DLL and settings
        if (!SystemContext.IsFullTrustLevel)
        {
            lblStatus.Text = ResHelper.GetString("socialnetworking.fulltrustrequired");
        }
        else if ((String.IsNullOrEmpty(consumerKey) || String.IsNullOrEmpty(consumerSecret)) && String.IsNullOrEmpty(oauthToken) && String.IsNullOrEmpty(error))
        {
            lblStatus.Text = ResHelper.GetString("socialnetworking.linkedin.apisettingsmissing");
        }
        else
        {
            // If access denied
            if (error.EqualsCSafe("user_refused"))
            {
                // Close the window
                StringBuilder script = new StringBuilder("if(wopener.setAccessTokenToTextBox){ CloseDialog(); }");

                ScriptHelper.RegisterStartupScript(Page, typeof(string), "TokenScript", ScriptHelper.GetScript(script.ToString()));
            }
            else
            {
                try
                {
                    // Authenticate and retrieve tokens
                    Dictionary <string, string> tokens = LinkedInProvider.Authorize(txtToken);
                    if (tokens.Count != 0)
                    {
                        // Return access token values and close the window
                        StringBuilder script = new StringBuilder("if(wopener.setAccessTokenToTextBox){ wopener.setAccessTokenToTextBox('")
                                               .AppendFormat("{0}', '{1}', '{2}'); CloseDialog(); }}", txtToken, tokens["AccessToken"], tokens["AccessTokenSecret"]);
                        ScriptHelper.RegisterStartupScript(Page, typeof(string), "TokenScript", ScriptHelper.GetScript(script.ToString()));
                    }
                    else
                    {
                        // Error occurred while communicating with LinkedIn
                        lblStatus.Text = ResHelper.GetString("socialnetworking.authorizationerror");
                    }
                }
                catch (Exception ex)
                {
                    LogAndShowError("SocialMedia", "LinkedInProvider", ex);
                }
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentMaster.Title.TitleText  = ResHelper.GetString("socialnetworking.linkedin.accesstoken");
        CurrentMaster.Title.TitleImage = GetImageUrl("Others/LinkedIn/linkedin.png");

        string txtToken       = QueryHelper.GetString("txtToken", String.Empty);
        string consumerKey    = QueryHelper.GetString("apiKey", String.Empty);
        string consumerSecret = QueryHelper.GetString("apiSecret", String.Empty);
        string oauthToken     = QueryHelper.GetString("oauth_token", String.Empty);
        string error          = QueryHelper.GetString("oauth_problem", String.Empty);
        Dictionary <string, string> tokens = null;

        // Check Social networking DLL and settings
        if (!SiteManagerFunctions.CheckSocialNetworkingDLL())
        {
            lblStatus.Text = ResHelper.GetString("socialnetworking.renameddll");
        }
        else if ((String.IsNullOrEmpty(consumerKey) || String.IsNullOrEmpty(consumerSecret)) && String.IsNullOrEmpty(oauthToken) && String.IsNullOrEmpty(error))
        {
            lblStatus.Text = ResHelper.GetString("socialnetworking.linkedin.apisettingsmissing");
        }
        else
        {
            // If access denied
            if (error.EqualsCSafe("user_refused"))
            {
                // Close the window
                StringBuilder script = new StringBuilder("if(wopener.setAccessTokenToTextBox){ CloseDialog(); }");

                ScriptHelper.RegisterStartupScript(Page, typeof(string), "TokenScript", ScriptHelper.GetScript(script.ToString()));
            }
            else
            {
                // Authenticate and retrieve tokens
                tokens = LinkedInProvider.Authorize(txtToken);
                if (tokens.Count != 0)
                {
                    // Return access token values and close the window
                    StringBuilder script = new StringBuilder("if(wopener.setAccessTokenToTextBox){ wopener.setAccessTokenToTextBox('")
                                           .AppendFormat("{0}', '{1}', '{2}'); CloseDialog(); }}", txtToken, tokens["AccessToken"], tokens["AccessTokenSecret"]);
                    ScriptHelper.RegisterStartupScript(Page, typeof(string), "TokenScript", ScriptHelper.GetScript(script.ToString()));
                }
                else
                {
                    // Error occured while communicating with LinkedIn
                    lblStatus.Text = ResHelper.GetString("socialnetworking.authorizationerror");
                }
            }
        }
    }
Ejemplo n.º 3
0
    protected void Page_Load(object sender, EventArgs e)
    {
        PageTitle.TitleText            = GetString("socialnetworking.linkedin.accesstoken");
        PageTitle.ShowFullScreenButton = false;
        PageTitle.ShowCloseButton      = false;

        var data = LinkedInProvider.GetLinkedInData();

        if (data.SettingsMissing)
        {
            lblStatus.Text = GetString("socialnetworking.linkedin.apisettingsmissing");
            return;
        }

        var returnUrl = new Uri(URLHelper.GetAbsoluteUrl(LinkedInProvider.ACCESS_TOKEN_PAGE));

        // User allowed access
        if (!String.IsNullOrEmpty(data.Code))
        {
            // Authenticate and retrieve tokens
            if (LinkedInProvider.Authorize(data, returnUrl, out var token))
            {
                // Return access token values and close the window
                var script = new StringBuilder("if(wopener.setAccessTokenToTextBox){ wopener.setAccessTokenToTextBox('")
                             .AppendFormat("{0}', '{1}'); CloseDialog(); }}", data.EditorId, token.AccessToken);
                ScriptHelper.RegisterStartupScript(Page, typeof(string), "TokenScript", ScriptHelper.GetScript(script.ToString()));
            }
            else
            {
                // Error occurred while communicating with LinkedIn
                lblStatus.Text = GetString("socialnetworking.authorizationerror");
            }

            return;
        }

        // User denied access
        if (data.UserDeniedAccess)
        {
            // Close the window
            var script = new StringBuilder("if(wopener.setAccessTokenToTextBox){ CloseDialog(); }");

            ScriptHelper.RegisterStartupScript(Page, typeof(string), "TokenScript", ScriptHelper.GetScript(script.ToString()));

            return;
        }

        LinkedInProvider.OpenAuthorizationPage(data, returnUrl);
    }
    /// <summary>
    /// Completes the authorization process. Access tokens and list of administrated companies are retrived and set to control that opened the dialog.
    /// </summary>
    private void CompleteAuthorization(ILinkedInData data, Uri url)
    {
        if (LinkedInProvider.Authorize(data, url, out var token))
        {
            List <CMS.SocialMarketing.LinkedInCompany> companies;
            try
            {
                companies = CMS.SocialMarketing.LinkedInHelper.GetUserCompanies(token.AccessToken);
            }
            catch (Exception ex)
            {
                LogAndShowError("LinkedInCompanyAccessToken", "GET_COMPANIES", ex);

                return;
            }

            string formattedExpiration = token.Expiration.HasValue ? TimeZoneHelper.ConvertToUserTimeZone(token.Expiration.Value, true, MembershipContext.AuthenticatedUser, SiteContext.CurrentSite) : String.Empty;
            string json = JsonConvert.SerializeObject(
                new
            {
                accessToken           = token.AccessToken,
                accessTokenSecret     = "",
                tokenExpiration       = token.Expiration.HasValue ? token.Expiration.Value.ToString("g", CultureInfo.InvariantCulture) : String.Empty,
                tokenExpirationString = formattedExpiration,
                tokenAppId            = Parameters["AppInfoId"],
                companies
            },
                new JsonSerializerSettings {
                StringEscapeHandling = StringEscapeHandling.EscapeHtml
            }
                );

            // Set retrieved access token to the opener window
            string script = String.Format(@"
if(wopener.linkedInCompanyControl && wopener.linkedInCompanyControl['{0}']) {{
    wopener.linkedInCompanyControl['{0}'].setData({1});
}}
CloseDialog();", Parameters["ClientID"], json);

            ScriptHelper.RegisterStartupScript(Page, typeof(string), "TokenScript", script, true);
        }
    }