public static string AuthenticateProfile(string login, string password, string recordTypeName) { string token = ""; using (Agility_UGC_API_WCFClient client = UGCAPIUtil.GetAPIClient("http://ugc.agilitycms.com/agility-ugc-api-wcf.svc", TimeSpan.FromSeconds(10))) { DataServiceAuthorization dsa = UGCAPIUtil.GetDataServiceAuthorization(-1); token = client.AuthenticateWithNamedFields(dsa, recordTypeName, login, password, "AgilityCommentsLoginID", "AgilityCommentsLoginPassword"); } return(token); }
public ActionResult Photo(int id, int w, int h, string config) { string photoUrl = string.Empty; try { using (Agility_UGC_API_WCFClient client = UGCAPIUtil.GetAPIClient("http://ugc.agilitycms.com/agility-ugc-api-wcf.svc", TimeSpan.FromSeconds(10))) { DataServiceAuthorization dsa = UGCAPIUtil.GetDataServiceAuthorization(-1); Record userProfile = client.GetRecord(dsa, id, FileStorage.EdgeURL); if (userProfile != null) { if (!string.IsNullOrEmpty(userProfile.GetValue("AgilityCommentsPhoto") as string)) { //if photo found, use this photoUrl = userProfile.GetValue("AgilityCommentsPhoto") as string; photoUrl = CommentsUtils.GetTranscodedUrl(photoUrl, w, h); return(Redirect(photoUrl)); } } } } catch (Exception ex) { Agility.Web.Tracing.WebTrace.WriteException(ex, string.Format("Error getting profile image for {0},", id)); } //no profile found or no photo found var configReferenceName = config; var configRepo = new AgilityContentRepository <AgilityContentItem>(configReferenceName); if (configRepo != null) { var defaultImage = configRepo.Item("").GetAttachment("DefaultAvatar"); if (defaultImage != null) { return(Redirect(CommentsUtils.GetTranscodedUrl(defaultImage.URL, w, h))); } } return(new EmptyResult()); }
public static List <AgilityContentItem> GetMostViewedBlogPosts(IAgilityContentRepository <AgilityContentItem> postsRepo, int take) { List <AgilityContentItem> posts = new List <AgilityContentItem>(); try { using (Agility_UGC_API_WCFClient client = UGCAPIUtil.GetAPIClient("http://ugc.agilitycms.com/agility-ugc-api-wcf.svc", TimeSpan.FromSeconds(10))) { DataServiceAuthorization dsa = UGCAPIUtil.GetDataServiceAuthorization(-1); var searchStats = client.GetStats(dsa, "WCM", "PageViews", postsRepo.ContentReferenceName, postsRepo.LanguageCode, -1, DateTime.Now.AddYears(-1), DateTime.Now); var results = searchStats.Items; var postsIDs = results.Take(take).Select(i => i.ItemID.ToString()); string postsIDsCommas = string.Join(",", postsIDs); posts = GetPosts(postsRepo, -1, postsIDsCommas, "", "", "", "", take, 0); } } catch (Exception ex) { Agility.Web.Tracing.WebTrace.WriteException(ex); return(null); } return(posts); }
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)); } }