using log4net; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using WebPortal.Models; using Pulsa.Texts; using WebService; using WebPortal.Extensions; using Microsoft.AspNetCore.Http; using System.Net; using System.IO; using Newtonsoft.Json; namespace WebPortal.Controllers { public class HomeController : BaseController { private ILog log = LogManager.GetLogger("HomeController"); private WsWebPortalClient wsClient = new WsWebPortalClient(); public HomeController(IConfiguration _configuration, IWebHostEnvironment hostEnvironment) : base(_configuration, hostEnvironment) { // init } public IActionResult Index(String noDetect, String tempMsisdn) { LoadBanner("HOME"); LoadTheme(); String msisdn = HttpContext.Session.GetString("account"); if (msisdn == null && noDetect == null) { if (tempMsisdn != null && tempMsisdn.Length > 0) { msisdn = tempMsisdn; ViewBag.msisdn = msisdn; } else { DetectMsisdn(wsClient); } } ViewBag.productType = ProductType.RECHARGE; return View(); } private void LoadTheme() { log.Info("Load theme"); // load theme SetWsClient(ref wsClient); wsGetCurrentThemeRequest wsRequest = new wsGetCurrentThemeRequest( BaseController.wsUser, BaseController.wsPassword); var wsResponse = wsClient.wsGetCurrentTheme(wsRequest); if (wsResponse.@return.listTheme != null && wsResponse.@return.listTheme.Length > 0) { log.Info("Current theme: " + wsResponse.@return.listTheme[0].themeName); HttpContext.Session.SetComplexData("currentTheme", wsResponse.@return.listTheme[0]); } } private void LoadBanner(String type) { LoadBanner(wsClient, type, "2"); } private string GetIp() { string ipRemote = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(); string ipLocal = HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString(); log.Info("Ip Remote: " + ipRemote + ", IP local: " + ipLocal); return ipRemote; } [HttpPost] [ValidateAntiForgeryToken] public JsonResult RechargeScratch(string msisdn, string code, string captcharesponse) { try { if (Check(captcharesponse) == false) { return Json(new { error = "16", content = Lang.CaptchaInvalid }); } wsBuyCardRequest req = new wsBuyCardRequest(wsUser, wsPassword, msisdn, code, "1", getCurrentLang()); SetWsClient(ref wsClient); var res = wsClient.wsBuyCard(req); return Json(new { error = res.@return.code, content = res.@return.message }); } catch (Exception ex) { log.Error("Exception RechargeScratch", ex); return Json(new { error = "-1", content = "System failed" }); } } private bool Check(string response) { //string Response = HttpContext.Current.Request.QueryString["g-recaptcha-response"];//Getting Response String Append to Post Method bool Valid = false; //Request to Google Server HttpWebRequest req = (HttpWebRequest)WebRequest.Create ("https://www.google.com/recaptcha/api/siteverify?secret=" + BaseController.CaptchaSecretKey + "&response=" + response); try { //Google recaptcha Response using (WebResponse wResponse = req.GetResponse()) { using (StreamReader readStream = new StreamReader(wResponse.GetResponseStream())) { string jsonResponse = readStream.ReadToEnd(); //JavaScriptSerializer js = new JavaScriptSerializer(); //MyObject data = js.Deserialize(jsonResponse);// Deserialize Json MyObject data = JsonConvert.DeserializeObject(jsonResponse); Valid = Convert.ToBoolean(data.success); } } return Valid; } catch (WebException ex) { throw ex; } } public class MyObject { public string success { get; set; } } } }