public virtual JsonResult DomainIdentificationRequest(string domainAddress) { string from = "APILOGINFO - " + HttpContext.Request.UserHostAddress; InsideWordWebLog.Instance.Buffer(from, "DomainIdentificationRequest(" + domainAddress + ")"); ApiMsgVM returnMessage = new ApiMsgVM(1); Uri domainUri = null; if (!IWStringUtility.TryUrlDecode(domainAddress, out domainAddress) || !Uri.TryCreate(domainAddress, UriKind.Absolute, out domainUri)) { returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure; returnMessage.StatusMessage = domainAddress + " is an invalid uri"; } else { ProviderDomain aDomain = new ProviderDomain(); ProviderIssuedKey issuedKey = new ProviderIssuedKey(); ProviderMember aMember = new ProviderMember(); if (aDomain.Load(domainUri.AbsoluteUri)) { aMember.Load(aDomain.MemberId); } else { // Domain doesn't exist already so create it and a member aMember.CreateDate = DateTime.UtcNow; aMember.EditDate = DateTime.UtcNow; aMember.Save(); aDomain.CreateDate = DateTime.UtcNow; aDomain.EditDate = DateTime.UtcNow; aDomain.Domain = domainUri; aDomain.IsValidated = false; aDomain.MemberId = aMember.Id.Value; aDomain.Save(); } issuedKey.LoadOrCreate(aMember.Id.Value, domainUri.AbsoluteUri, true, 1, false); returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.success; returnMessage.StatusMessage = "Success"; returnMessage.Content = issuedKey.IssuedKey; } InsideWordWebLog.Instance.Buffer(from, "Done DomainIdentificationRequest - " + returnMessage); return Json(returnMessage); }
public virtual JsonResult DomainIdentification(string domainAddress, string subFolder) { string from = "APILOGINFO - " + HttpContext.Request.UserHostAddress; InsideWordWebLog.Instance.Buffer(from, "DomainIdentification(" + domainAddress + ", " + subFolder + ")"); ApiMsgVM returnMessage = new ApiMsgVM((int)ApiMsgVM.StatusEnum.failure); string subFolderDecoded = null; IWStringUtility.TryUrlDecode(subFolder, out subFolderDecoded, ""); Uri domainUri = null; Uri pathUri = null; if (!IWStringUtility.TryUrlDecode(domainAddress, out domainAddress) || !Uri.TryCreate(domainAddress, UriKind.Absolute, out domainUri)) { returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure; returnMessage.StatusMessage = domainAddress + " is an invalid uri"; } else if (!IWStringUtility.TryUriConcat(domainUri, subFolderDecoded, out pathUri)) { returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure; returnMessage.StatusMessage = domainUri.AbsoluteUri + " and " + subFolder + " form an invalid uri"; } else { ProviderDomain aDomain = new ProviderDomain(); ProviderIssuedKey issuedKey = new ProviderIssuedKey(); ProviderMember aMember = new ProviderMember(); if (!aDomain.Load(domainUri.AbsoluteUri)) { returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure; returnMessage.StatusMessage = domainUri.AbsoluteUri +" does not exist in our system. Use " +Url.Action(MVC.API.DomainIdentificationRequest()) +" to request a key and identify yourself first."; } else if(!aMember.Load(aDomain.MemberId)) { returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure; returnMessage.StatusMessage = "The member associated with this domain, " +domainUri.AbsoluteUri +", does not exist. Contact support to resolve this issue."; } else if (!issuedKey.LoadBy(aMember.Id.Value, domainUri.AbsoluteUri, true, 1)) { returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure; returnMessage.StatusMessage = "Your issued key has been used up already or was never issued. Use " + Url.Action(MVC.API.DomainIdentificationRequest()) + " to request a new key for identification."; } else { // all the data is good and we're ready to check if the key has been placed in the correct uri. bool isFetchSuccess = false; string htmlPage = null; HtmlDocument htmlDoc = new HtmlDocument(); try { using (WebClient client = new WebClient()) { // TODO: DOS attack is possible here by sending us to a page with a gig of data. // put some sort of precautionary check here to avoid loading too much data. htmlPage = client.DownloadString(pathUri.AbsoluteUri); } htmlDoc.LoadHtml(htmlPage); isFetchSuccess = true; } catch (Exception caughtException) { returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure; returnMessage.StatusMessage = "Failed to read the webpage at " + pathUri.AbsoluteUri; isFetchSuccess = false; } if (isFetchSuccess) { HtmlNode embeddedIssuedKey = htmlDoc.GetElementbyId(issuedKey.IssuedKey); if (embeddedIssuedKey == null || embeddedIssuedKey.Name.CompareTo("input") != 0) { returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.failure; returnMessage.StatusMessage = "Could not find hidden input tag with id containing the issued key at page " + pathUri.AbsoluteUri; } else { //we found it so let's validate the domain and return the issued keys aDomain.IsValidated = true; aDomain.EditDate = DateTime.UtcNow; aDomain.Save(); returnMessage.StatusCode = (int)ApiMsgVM.StatusEnum.success; returnMessage.StatusMessage = "You have been successfully validated. Here are the issued keys for this month and next months. Do not share these with anyone."; returnMessage.Content = aMember.CurrentMonthIssuedKey.IssuedKey + "," + aMember.NextMonthIssuedKey.IssuedKey; // decommission the issued key issuedKey.TryDecommission(); } } } } InsideWordWebLog.Instance.Buffer(from, "Done DomainIdentification - " + returnMessage); return Json(returnMessage); }