public ActionResult Comments(AgilityContentItem item)
        {
            var dynamicItem         = ResolveBlogPostDetails();
            var configReferenceName = item["CommentsConfiguration"] as string;
            var config = new AgilityContentRepository <AgilityContentItem>(configReferenceName).Item("");

            if (config == null || dynamicItem == null || string.IsNullOrEmpty(dynamicItem.CommentsRecordTypeName()))
            {
                return(null);
            }

            var model = new CommentsViewModel();

            model.CommentsRecordTypeName = dynamicItem.CommentsRecordTypeName();
            model.RelatedContentID       = dynamicItem.ContentID;
            model.Config = config;
            model.FacebookLoginEnabled = (CommentsUtils.GetBool(config["EnableFacebookLogin"]) && !string.IsNullOrEmpty(config["FacebookAppID"] as string));
            model.TwitterLoginEnabled  = (CommentsUtils.GetBool(config["EnableTwitterLogin"]) && !string.IsNullOrEmpty(config["TwitterAPIConsumerKey"] as string) && !string.IsNullOrEmpty(config["TwitterAPIConsumerSecret"] as string));
            model.GuestLoginEnabled    = CommentsUtils.GetBool(config["EnableGuestLogin"]);

            return(PartialView(AgilityComponents.TemplatePath("Comments-Module"), model));
        }
        public ActionResult TwitterRedirectStep2(string config, string callback)
        {
            HttpContext.Response.Cache.VaryByParams["*"] = true;
            string htmlOutput = "";

            try
            {
                string configReferenceName = config;
                if (string.IsNullOrEmpty(configReferenceName) || new AgilityContentRepository <AgilityContentItem>(configReferenceName).Item("") == null)
                {
                    throw new HttpException(500, string.Format("Agility Comments Configuration Item '{0}' not found", configReferenceName));
                }

                var    configItem       = new AgilityContentRepository <AgilityContentItem>(configReferenceName).Item("");
                string token            = Request.QueryString["oauth_token"];
                string verifier         = Request.QueryString["oauth_verifier"];
                string callbackFunction = callback;

                Uri rq = new Uri("https://api.twitter.com/oauth/access_token");

                OAuthBase oauth = new OAuthBase();

                string timestamp = oauth.GenerateTimeStamp();
                string nonce     = oauth.GenerateNonce();

                string consumerKey    = configItem["TwitterAPIConsumerKey"] as string;
                string consumerSecret = configItem["TwitterAPIConsumerSecret"] as string;

                var postData = string.Format("{0}={1}", "oauth_verifier", verifier);

                var authorizationHeader = oauth.CreateAuthenticationAccessHeader(rq.ToString(), "POST", consumerKey, consumerSecret, token, new Dictionary <string, string> {
                    { "oauth_verifier", verifier }
                });

                WebClient client = new WebClient();

                client.Headers.Add("Authorization", authorizationHeader);

                string response = client.UploadString(rq, postData);

                var retData = HttpUtility.ParseQueryString(response);
                token = retData["oauth_token"];
                string secret     = retData["oauth_token_secret"];
                string userID     = retData["user_id"];
                string screenName = retData["screen_name"];

                if (!string.IsNullOrWhiteSpace(secret) &&
                    !string.IsNullOrWhiteSpace(token) &&
                    !string.IsNullOrWhiteSpace(screenName))
                {
                    //make sure everything came back ok...

                    timestamp = oauth.GenerateTimeStamp();
                    nonce     = oauth.GenerateNonce();

                    //get the fullname and other details as well...
                    string requestUrl = string.Format("https://api.twitter.com/1.1/account/verify_credentials.json");

                    //can't get email unless app is whitelisted https://dev.twitter.com/rest/reference/get/account/verify_credentials
                    if (CommentsUtils.GetBool(configItem["TwitterRequestEmail"]))
                    {
                        requestUrl += "?include_email=true";
                    }

                    rq = new Uri(requestUrl);

                    authorizationHeader             = oauth.CreateAuthenticationAccessHeader(rq.ToString(), "GET", consumerKey, consumerSecret, token, new Dictionary <string, string>(), secret);
                    client.Headers["Authorization"] = authorizationHeader;
                    response = client.DownloadString(rq);

                    TwitterUser tUser = CommentsUtils.DeserializeJSONObject <TwitterUser>(response);

                    string name = tUser.name;
                    string twitterProfileImage = tUser.profile_image_url;
                    string email = tUser.email;

                    //save the larger version of the profile image
                    twitterProfileImage = twitterProfileImage.Replace("_normal", "_bigger");


                    StringBuilder html = new StringBuilder();
                    html.Append("<html>");
                    html.AppendLine("<head>");
                    html.AppendLine("<title>Authenticating...</title>");
                    html.AppendLine("</head>");
                    html.AppendLine("<body>");
                    if (!string.IsNullOrEmpty(userID))
                    {
                        html.AppendLine("<script>");
                        html.AppendLine("if(window.opener) {");
                        html.AppendFormat("window.opener.{5}(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\");", userID, screenName, name, twitterProfileImage, email, callbackFunction);
                        html.AppendLine("window.close()");
                        html.AppendLine("} else {");
                        html.AppendLine("document.write(\"An error occurred during the authenticatin process. Please close your browser and try again.\");");
                        html.AppendLine("}");
                        html.AppendLine("</script>");
                    }
                    else
                    {
                        html.AppendLine("<script>window.close()</script>");
                    }

                    html.AppendLine("</body>");
                    html.AppendLine("</html>");

                    htmlOutput = html.ToString();
                }

                return(Content(htmlOutput));
            }
            catch (Exception ex)
            {
                Agility.Web.Tracing.WebTrace.WriteException(ex);
                return(Content(ex.Message));
            }
        }