public ActionResult Create(Group model) { int id = WebSecurity.GetUserId(WebSecurity.CurrentUserName); var userProfile = _userContext.UserProfiles.First(x => x.UserId == id); if (string.IsNullOrWhiteSpace(userProfile.PrivateKey) || string.IsNullOrWhiteSpace(userProfile.PublicKey)) { TempData["Notification"] = new Notification("Please provide access keys that have been sent you by email", Nature.warning); return RedirectToAction("Account", "Settings"); } if (ModelState.IsValid) { UserData userData = new UserData(); userData.PublicKey = userProfile.PublicKey; userData.Timestamp = DateTime.Now; userData.GenerateAuthenticationHash(userProfile.PrivateKey + userProfile.PublicKey + "POST/contact" + userData.Timestamp + userProfile.PrivateKey); GroupEndpoint g = new GroupEndpoint(); model.isContactGroup = true; string message = g.CreateGroup(model, userData); TempData["Notification"] = new Notification("Group has been added" + message, Nature.success); Thread.Sleep(2500); return RedirectToAction("Index"); } else { return View(model); } }
public string CreateGroup(Group model, UserData userData) { try { client = new HttpClient(); var postData = new List<KeyValuePair<string, string>>(); postData.Add(new KeyValuePair<string, string>("name", model.name)); postData.Add(new KeyValuePair<string, string>("description", model.description)); postData.Add(new KeyValuePair<string, string>("isContactGroup", "true")); postData.Add(new KeyValuePair<string, string>("tags", model.tags[0])); if (!string.IsNullOrWhiteSpace(model.parentId)) { postData.Add(new KeyValuePair<string, string>("parentId", model.parentId)); } HttpContent content = new FormUrlEncodedContent(postData); content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); content.Headers.Add("Timestamp", userData.Timestamp.ToString()); content.Headers.Add("Digest", userData.AuthenticationHash); content.Headers.Add("Public-Key", userData.PublicKey); client.PostAsync("http://localhost:3000/contact", content) .ContinueWith(postTask => { postTask.Result.EnsureSuccessStatusCode(); var result = postTask.Result.Content.ReadAsStringAsync(); client.Dispose(); return result; }); } catch (Exception ex) { throw ex; } return null; }
public ActionResult Create() { Group g = new Group(); return View(g); }