SubscriberController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. using log4net;
  2. using LuckyCallWebCore.Source;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Configuration;
  7. using ServiceAPI;
  8. using System;
  9. using LuckyCallWebCore.Extensions;
  10. using LuckyCallWebCore.Models;
  11. using Microsoft.Extensions.Primitives;
  12. using Newtonsoft.Json;
  13. using System.ServiceModel;
  14. using System.ServiceModel.Channels;
  15. using System.Threading.Tasks;
  16. namespace LuckyCallWebCore.Controllers
  17. {
  18. public class SubscriberController : BaseController
  19. {
  20. private static ILog log = LogManager.GetLogger("SubscriberController");
  21. ServiceAPI.WsLuckyCallClient wsClient = new ServiceAPI.WsLuckyCallClient();
  22. [HttpPost]
  23. [ValidateAntiForgeryToken]
  24. public JsonResult Subscribe(String msisdn, String packName)
  25. {
  26. if (msisdn == null || msisdn.Trim() == "")
  27. {
  28. msisdn = HttpContext.Session.GetString("msisdn");
  29. }
  30. if (msisdn != "" && msisdn != null)
  31. {
  32. msisdn = validateMsisdn(msisdn);
  33. SetWsClient(ref wsClient, msisdn);
  34. //ServiceAPI.response subInfo = wsClient.wsGetSubInfo(wsUser, wsPassword, msisdn);
  35. ServiceAPI.response res = null;
  36. res = wsClient.wsRegisterSubOtp(wsUser, wsPassword, msisdn, packName);
  37. if (res.errorCode == "0")
  38. {
  39. // success
  40. }
  41. else if (res.errorCode == "100")
  42. {
  43. // need confirm
  44. RequestTemp reqTemp = new RequestTemp();
  45. reqTemp.packName = packName;
  46. reqTemp.requestId = res.resultCode;
  47. reqTemp.msisdn = msisdn;
  48. Session["reqTemp"] = reqTemp;
  49. }
  50. return Json(new
  51. {
  52. error = res.errorCode,
  53. content = res.content,
  54. msisdn = msisdn.Substring(3)
  55. });
  56. }
  57. else
  58. {
  59. return Json(new
  60. {
  61. error = "-1",
  62. content = Lang.InvalidMsisdn
  63. });
  64. }
  65. }
  66. [ValidateAntiForgeryToken]
  67. public JsonResult SubscribeConfirm(String otp)
  68. {
  69. RequestTemp reqTemp = Session["reqTemp"] as RequestTemp;
  70. if (reqTemp == null)
  71. {
  72. return Json(new
  73. {
  74. error = "-1",
  75. content = "Timeout",
  76. href = "/Home/Index"
  77. });
  78. }
  79. String msisdn = reqTemp.msisdn;
  80. if (msisdn != "" && msisdn != null)
  81. {
  82. CommonUtils.SetWsClient(ref wsClient, Session.SessionID);
  83. //ServiceAPI.response subInfo = wsClient.wsGetSubInfo(wsUser, wsPassword, msisdn);
  84. ServiceAPI.response res = null;
  85. String channel = ConfigurationManager.AppSettings["reddemChannel"];
  86. res = wsClient.wsRegisterSubConfirm(wsUser, wsPassword, msisdn, reqTemp.packName, otp);
  87. if (res.errorCode == "0")
  88. {
  89. // login success --> store session
  90. Session["msisdn"] = msisdn;
  91. Session["account"] = wsClient.wsGetSubInfo(wsUser, wsPassword, msisdn);
  92. }
  93. return Json(new
  94. {
  95. error = res.errorCode,
  96. content = res.content,
  97. msisdn = msisdn.Substring(3)
  98. });
  99. }
  100. else
  101. {
  102. return Json(new
  103. {
  104. error = "-1",
  105. content = "Timeout"
  106. });
  107. }
  108. }
  109. [HttpPost]
  110. [ValidateAntiForgeryToken]
  111. public JsonResult LoginAction(String phoneNumber, string password, string captcharesponse)
  112. {
  113. String msisdn = CommonUtils.validateMsisdn(phoneNumber);
  114. CheckSessionRes checkSession = CheckSessionValid(ActionSession.Login);
  115. if (checkSession.errorCode != "0")
  116. {
  117. return Json(new
  118. {
  119. error = checkSession.errorCode,
  120. content = checkSession.content,
  121. msisdn = msisdn.Substring(3)
  122. });
  123. }
  124. if (msisdn != "")
  125. {
  126. ServiceAPI.response subInfo = null;
  127. CommonUtils.SetWsClient(ref wsClient, Session.SessionID);
  128. ServiceAPI.response res = wsClient.wsLogin(wsUser, wsPassword, password, msisdn);
  129. if (res.errorCode == "0")
  130. {
  131. // login success --> store session
  132. Session["msisdn"] = msisdn;
  133. subInfo = wsClient.wsGetSubInfo(wsUser, wsPassword, msisdn);
  134. Session["account"] = subInfo;
  135. LoginSuccessAction();
  136. }
  137. else
  138. {
  139. LoginFailAction();
  140. }
  141. if (subInfo != null)
  142. {
  143. return Json(new
  144. {
  145. error = res.errorCode,
  146. content = res.content,
  147. msisdn = msisdn.Substring(3)
  148. });
  149. }
  150. else
  151. {
  152. return Json(new
  153. {
  154. error = res.errorCode,
  155. content = res.content,
  156. msisdn = msisdn.Substring(3)
  157. });
  158. }
  159. }
  160. else
  161. {
  162. return Json(new
  163. {
  164. error = "-1",
  165. content = Lang.InvalidMsisdn
  166. });
  167. }
  168. }
  169. [ValidateAntiForgeryToken]
  170. public JsonResult SignupAction(String phoneNumber, string captcharesponse)
  171. {
  172. //if (Check(captcharesponse) == false)
  173. //{
  174. // return Json(new
  175. // {
  176. // error = "16",
  177. // content = Lang.CaptchaInvalid
  178. // });
  179. //}
  180. CheckSessionRes checkSession = CheckSessionValid(ActionSession.SignUp);
  181. if (checkSession.errorCode != "0")
  182. {
  183. return Json(new
  184. {
  185. error = checkSession.errorCode,
  186. content = checkSession.content
  187. });
  188. }
  189. String msisdn = CommonUtils.validateMsisdn(phoneNumber);
  190. if (msisdn != "")
  191. {
  192. CommonUtils.SetWsClient(ref wsClient, Session.SessionID);
  193. ServiceAPI.response res = wsClient.wsCreateAccount(wsUser, wsPassword, "", msisdn, "1");
  194. if (res.errorCode == "0")
  195. {
  196. // signup success --> store session
  197. SetLastSignUp();
  198. Session["msisdn"] = msisdn;
  199. ViewBag.msisdn = msisdn;
  200. CommonUtils.SetWsClient(ref wsClient, Session.SessionID);
  201. ServiceAPI.response subInfo = wsClient.wsGetSubInfo(wsUser, wsPassword, msisdn);
  202. Session["account"] = subInfo;
  203. }
  204. return Json(new
  205. {
  206. error = res.errorCode,
  207. content = res.content,
  208. msisdn = msisdn.Substring(3)
  209. });
  210. }
  211. else
  212. {
  213. return Json(new
  214. {
  215. error = "-1",
  216. content = Lang.InvalidMsisdn
  217. });
  218. }
  219. }
  220. [ValidateAntiForgeryToken]
  221. public JsonResult ResetPassword(String phoneNumber, string captcharesponse)
  222. {
  223. //if (Check(captcharesponse) == false)
  224. //{
  225. // return Json(new
  226. // {
  227. // error = "16",
  228. // content = Lang.CaptchaInvalid
  229. // });
  230. //}
  231. CheckSessionRes checkSession = CheckSessionValid(ActionSession.Resetpass);
  232. if (checkSession.errorCode != "0")
  233. {
  234. return Json(new
  235. {
  236. error = checkSession.errorCode,
  237. content = checkSession.content
  238. });
  239. }
  240. String wsUser = ConfigurationManager.AppSettings["wsUser"];
  241. String wsPassword = ConfigurationManager.AppSettings["wsPassword"];
  242. String msisdn = CommonUtils.validateMsisdn(phoneNumber);
  243. if (msisdn != "")
  244. {
  245. ServiceAPI.WsMillionClient wsClient = new ServiceAPI.WsMillionClient();
  246. CommonUtils.SetWsClient(ref wsClient, Session.SessionID);
  247. ServiceAPI.response res = wsClient.wsResetPassword(wsUser, wsPassword, msisdn);
  248. SetLastResetPass();
  249. return Json(new
  250. {
  251. error = res.errorCode,
  252. content = res.content
  253. });
  254. }
  255. else
  256. {
  257. return Json(new
  258. {
  259. error = "-1",
  260. content = Lang.InvalidMsisdn
  261. });
  262. }
  263. }
  264. [HttpPost]
  265. public JsonResult CancelService()
  266. {
  267. String msisdn = Session["msisdn"] as String;
  268. if (msisdn != "" && msisdn != null)
  269. {
  270. wsClient.Endpoint.Address = GetWsEndpoint();
  271. var res = wsClient.wsCancelService(wsUser, wsPassword, msisdn);
  272. if (res.errorCode == "0")
  273. {
  274. Session["account"] = wsClient.wsGetSubInfo(wsUser, wsPassword, msisdn);
  275. }
  276. return Json(new
  277. {
  278. error = res.errorCode,
  279. content = res.content,
  280. msisdn = msisdn.Substring(3)
  281. });
  282. }
  283. else
  284. {
  285. return Json(new
  286. {
  287. error = "-1"
  288. });
  289. }
  290. }
  291. [HttpPost]
  292. public JsonResult BuyTurnOtp(String numTurn)
  293. {
  294. String msisdn = Session["msisdn"] as String;
  295. if (msisdn != "" && msisdn != null)
  296. {
  297. log.Info("BuyTurn " + msisdn + ", numTurn: " + numTurn);
  298. wsClient.Endpoint.Address = GetWsEndpoint();
  299. response res = wsClient.wsBuyTurnOtp(wsUser, wsPassword, msisdn, numTurn);
  300. return Json(new
  301. {
  302. error = res.errorCode,
  303. content = res.content,
  304. msisdn = msisdn.Substring(3),
  305. numTurn = numTurn
  306. });
  307. }
  308. else
  309. {
  310. return Json(new
  311. {
  312. error = "-1"
  313. });
  314. }
  315. }
  316. [HttpPost]
  317. public JsonResult BuyTurnConfirm(String otp, String numTurn)
  318. {
  319. String msisdn = Session["msisdn"] as String;
  320. if (msisdn != "" && msisdn != null)
  321. {
  322. log.Info("BuyTurnConfirm " + msisdn + ", numTurn: " + numTurn);
  323. wsClient.Endpoint.Address = GetWsEndpoint();
  324. response res = wsClient.wsBuyTurn(wsUser, wsPassword, msisdn, numTurn, otp);
  325. log.Info("BuyTurnConfirm " + msisdn + ": " + res.errorCode);
  326. if (res.errorCode == "0")
  327. {
  328. Session["account"] = wsClient.wsGetSubInfo(wsUser, wsPassword, msisdn);
  329. }
  330. return Json(new
  331. {
  332. error = res.errorCode,
  333. content = res.content,
  334. msisdn = msisdn.Substring(3)
  335. });
  336. }
  337. else
  338. {
  339. return Json(new
  340. {
  341. error = "-1"
  342. });
  343. }
  344. }
  345. public ActionResult Login()
  346. {
  347. return View();
  348. }
  349. public RedirectResult Logout()
  350. {
  351. Session.Clear();
  352. return Redirect("/Home?detecting=0");
  353. }
  354. private bool Check(string response)
  355. {
  356. //string Response = HttpContext.Current.Request.QueryString["g-recaptcha-response"];//Getting Response String Append to Post Method
  357. bool Valid = false;
  358. //Request to Google Server
  359. HttpWebRequest req = (HttpWebRequest)WebRequest.Create
  360. (" https://www.google.com/recaptcha/api/siteverify?secret=" + CommonUtils.CaptchaSecretKey + "&response=" + response);
  361. try
  362. {
  363. //Google recaptcha Response
  364. using (WebResponse wResponse = req.GetResponse())
  365. {
  366. using (StreamReader readStream = new StreamReader(wResponse.GetResponseStream()))
  367. {
  368. string jsonResponse = readStream.ReadToEnd();
  369. JavaScriptSerializer js = new JavaScriptSerializer();
  370. MyObject data = js.Deserialize<MyObject>(jsonResponse);// Deserialize Json
  371. Valid = Convert.ToBoolean(data.success);
  372. }
  373. }
  374. return Valid;
  375. }
  376. catch (WebException ex)
  377. {
  378. throw ex;
  379. }
  380. }
  381. public class MyObject
  382. {
  383. public string success { get; set; }
  384. }
  385. }
  386. }