public ActionResult Login(string recordType)
        {
            HttpContext.Response.Cache.VaryByParams["*"] = true;

            string profileRecordTypeName = recordType;
            string cacheKey = string.Format("AgilityCommentsProfileSearch_{0}", profileRecordTypeName);

            string name = Request["AgilityCommentsName"];

            string loginID          = Request["AgilityCommentsLoginID"];
            string externalPhotoUrl = Request["AgilityCommentsPhoto"];
            string copiedPhotoUrl   = "";
            string loginType        = Request["AgilityCommentsLoginType"];
            string email            = Request["AgilityCommentsEmail"];
            string search           = string.Format("AgilityCommentsLoginID = '{0}'", loginID);


            using (Agility_UGC_API_WCFClient client = UGCAPIUtil.GetAPIClient("http://ugc.agilitycms.com/agility-ugc-api-wcf.svc", TimeSpan.FromSeconds(10)))
            {
                int recordID = 0;
                DataServiceAuthorization dsa = UGCAPIUtil.GetDataServiceAuthorization(-1);

                LoginResult returnObject = new LoginResult();
                returnObject.isError = false;
                try
                {
                    var searchArg = new RecordSearchArg()
                    {
                        RecordTypeName = profileRecordTypeName,
                        Search         = search,
                        PageSize       = 1,
                        Columns        = new List <string>()
                        {
                            "AgilityCommentsName",
                            "AgilityCommentsLoginPassword",
                            "AgilityCommentsEmail",
                            "AgilityCommentsPhoto",
                            "AgilityCommentsExternalPhoto",
                            "AgilityCommentsLoginID",
                            "AgilityCommentsLoginType"
                        },
                        CacheKey = cacheKey
                    };

                    var res = client.SearchRecords(dsa, searchArg);

                    //generate the user's password based on the data we have
                    string password = FormsAuthentication.HashPasswordForStoringInConfigFile(string.Format("{0}_{1}", loginType, loginID), "sha1");

                    if (res.TotalRecords == 0)
                    {
                        //the user doesn't exist yet... create
                        Record userProfile = new Record()
                        {
                            RecordTypeName = profileRecordTypeName
                        };
                        userProfile["AgilityCommentsName"]          = name;
                        userProfile["AgilityCommentsLoginPassword"] = password;
                        userProfile["AgilityCommentsLoginID"]       = loginID;
                        userProfile["AgilityCommentsLoginType"]     = loginType;
                        if (!string.IsNullOrEmpty(email))
                        {
                            userProfile["AgilityCommentsEmail"] = email;
                        }

                        //if they have a photo from a social login
                        if (!string.IsNullOrEmpty(externalPhotoUrl))
                        {
                            copiedPhotoUrl = CommentsUtils.CopyAndUploadPhoto(externalPhotoUrl);

                            //save the new copied url (stored in blob storage now)
                            //this could be empty if there was a network error which will force the same action again next time they login
                            userProfile["AgilityCommentsPhoto"]         = copiedPhotoUrl;
                            userProfile["AgilityCommentsExternalPhoto"] = externalPhotoUrl;
                        }


                        recordID = client.SaveRecord(dsa, userProfile, cacheKey);
                    }
                    else
                    {
                        bool updateProfile = false;
                        var  record        = new Record();
                        record.ID             = res.Records[0].ID;
                        record.RecordTypeName = profileRecordTypeName;

                        copiedPhotoUrl = res.Records[0]["AgilityCommentsPhoto"] as string;
                        string oldExternalPhotoUrl = res.Records[0]["AgilityCommentsExternalPhoto"] as string;

                        if (name != null && !name.Equals(res.Records[0]["AgilityCommentsName"] as string, StringComparison.InvariantCultureIgnoreCase))
                        {
                            record["AgilityCommentsName"] = name;
                            updateProfile = true;
                        }

                        //save the record with the new password if we have to
                        if (string.IsNullOrWhiteSpace(res.Records[0]["AgilityCommentsLoginPassword"] as string))
                        {
                            record["AgilityCommentsLoginPassword"] = password;
                            updateProfile = true;
                        }

                        //save the record with the new password if we have to
                        if (string.IsNullOrWhiteSpace(res.Records[0]["AgilityCommentsEmail"] as string) && !string.IsNullOrEmpty(email))
                        {
                            record["AgilityCommentsEmail"] = email;
                            updateProfile = true;
                        }
                        else
                        {
                            email = res.Records[0]["AgilityCommentsEmail"] as string;
                        }

                        //if Photo is blank and we have one from social login, then replace
                        if ((string.IsNullOrEmpty(copiedPhotoUrl) && !string.IsNullOrEmpty(externalPhotoUrl)) || (!string.IsNullOrEmpty(externalPhotoUrl)) && (!externalPhotoUrl.Equals(oldExternalPhotoUrl)))
                        {
                            copiedPhotoUrl = CommentsUtils.CopyAndUploadPhoto(externalPhotoUrl);

                            //save the new copied url (stored in blob storage now)
                            //this could be empty if there was a network error which will force the same action again next time they login
                            record["AgilityCommentsPhoto"]         = copiedPhotoUrl;
                            record["AgilityCommentsExternalPhoto"] = externalPhotoUrl;
                            updateProfile = true;
                        }
                        else
                        {
                            copiedPhotoUrl = res.Records[0]["AgilityCommentsPhoto"] as string;
                        }

                        //update profile if neccessary
                        if (updateProfile)
                        {
                            client.SaveRecord(dsa, record);
                        }

                        recordID = res.Records[0].ID;
                    }

                    //log the user in as this user and return the token...
                    string token = AuthenticateProfile(loginID, password, profileRecordTypeName);
                    if (recordID > 0)
                    {
                        returnObject.recordID = recordID;
                    }

                    if (!string.IsNullOrEmpty(email))
                    {
                        returnObject.email = email;
                    }

                    if (!string.IsNullOrEmpty(copiedPhotoUrl))
                    {
                        returnObject.photo = copiedPhotoUrl;
                    }

                    returnObject.token = token;
                }
                catch (Exception ex)
                {
                    returnObject.message = string.Format("An error occurred while logging in: {0}", ex.Message);
                    returnObject.isError = true;
                }

                return(Json(returnObject, JsonRequestBehavior.AllowGet));
            }
        }
        /// <summary>
        /// Used to search UGC
        /// Returns a paged results
        /// </summary>
        /// <param name="recordTypeName"></param>
        /// <param name="searchString"></param>
        /// <returns></returns>
        public static PagedResult Search(string recordTypeName, string searchString, RecordState searchState = RecordState.Published, int recordCount = 10, int recordOffset = 0)
        {
            PagedResult results = new PagedResult();

            try
            {
                using (Agility_UGC_API_WCFClient client = UGCAPIUtil.APIClient)
                {
                    DataServiceAuthorization auth = UGCAPIUtil.GetDataServiceAuthorization(-1);

                    RecordSearchArg arg = new RecordSearchArg()
                    {
                        RecordTypeName = recordTypeName,
                        Search = searchString,
                        State = searchState,
                        PageSize = recordCount,
                        RecordOffset = recordOffset
                    };

                    //fetch results
                    results = client.SearchRecords(auth, arg);

                }
            }
            catch (Exception ex)
            {
                Utilities.LogEvent(string.Format(ex.Message));
            }

            return results;
        }