| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353 |
- using NEducation.Code;
- using NEducation.Content.Texts;
- using NEducation.Models;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.ServiceModel;
- using System.Web;
- using System.Web.Mvc;
- using System.IO;
- using System.Globalization;
- using System.Linq;
- using System.Threading;
- using System.Web.SessionState;
- using Newtonsoft.Json;
- using System.Net.Sockets;
- using System.Net;
- using System.Security.Policy;
- using Microsoft.Extensions.Configuration;
- using System.Threading.Tasks;
- using System.Net.Http;
- using Microsoft.EntityFrameworkCore.Metadata.Internal;
- namespace NEducation.Controllers
- {
- public class HomeController : BaseController
- {
- private static log4net.ILog log { get; set; } = log4net.LogManager.GetLogger(typeof(HomeController));
- GenKeyWs.WsGenKeyClient wsClient = new GenKeyWs.WsGenKeyClient();
- public ActionResult Index(String token)
- {
- HomeModel model = new HomeModel();
- if (token != null)
- {
- Session["navitab"] = "Home";
- //HomeModel model = new HomeModel();
- // nhan dien thue bao
- String msisdnAuto = null;
- model.uuid = token;
- log.Info("uuid: " + token);
- HttpCookie reqCookies = Request.Cookies["onapp"];
- if (reqCookies != null)
- {
- string isOnApp = reqCookies["isonapp"].ToString();
- if (isOnApp == "1")
- {
- Session["WHICHDEVICE"] = UtilsController.Constant.REGISTER_ON_APP;
- }
- }
- String msisdnToken = getUserFromToken(token);
- log.Info("msisdn: " + msisdnToken);
- if (msisdnToken != "")
- {
- //String resDetect = CheckAutoLogin(log, msisdnToken);
- //log.Info("token: " + token + " res: " + resDetect);
- //if (resDetect != null)
- //{
- // dynamic json = JsonConvert.DeserializeObject(resDetect);
- // log.Info("res json: " + json);
- // String code = json["code"];
- // if (code == "200")
- // {
- // String errorCode = json["errorCode"];
- // if (errorCode == "200")
- // {
- // msisdnAuto = json["data"]["msisdn"];
- // log.Info("msisdnAuto: " + msisdnAuto);
- // }
-
- // auto login
- // reload user info
- HttpContext.Session.Remove("regInfos");
- Session["msisdnDetect"] = msisdnToken;
- // dang ky tren app
- Session["WHICHDEVICE"] = UtilsController.Constant.REGISTER_ON_APP;
- HttpCookie onApp = new HttpCookie("onapp");
- onApp["isonapp"] = "1";
- onApp.Expires = DateTime.Now.AddDays(30);
- Response.Cookies.Add(onApp);
- // check user has account
- GetUserProfileReq req = new GetUserProfileReq();
- req.users = msisdnToken;
- String rs = UtilsController.SendPost(req, System.Web.HttpContext.Current.Session.SessionID, UtilsController.WsType.UsersGetProfile);
- UserProfile profile = new UserProfile(rs);
- if (profile.id != null)
- {
- System.Web.HttpContext.Current.Session["profile"] = profile;
- // create new auth
- CreateAuthToken();
- String sessionId = Session.SessionID;
- // login success --> store session
- Session["msisdn"] = msisdnToken;
- Session["Token"] = token;
- // get sub
- UtilsController.ReloadSubInfo();
- // all info of user was loaded from server
- //UserProfile profile = Session["profile"] as UserProfile;
- String typeLanguage = Session["typeLanguage"] as String;
- }
- else
- {
- // Create a free account for the user
- UserRequest userRequest = new UserRequest
- {
- users = msisdnToken,
- msisdn = msisdnToken,
- command = "REGIST",
- channel = "WEB"
- };
- string rsRegister = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.UsersRegister);
- UserActionResult resRegister = new UserActionResult(rsRegister);
- Session["isSub"] = "false";
- if (resRegister.status == UtilsController.Constant.SUCCESS)
- {
- // Load profile
- GetUserProfileReq reqProfile = new GetUserProfileReq { users = msisdnToken };
- string rsProfile = UtilsController.SendPost(reqProfile, Session.SessionID, UtilsController.WsType.UsersGetProfile);
- UserProfile profileGet = new UserProfile(rsProfile);
- if (profileGet.id != null)
- {
- Session["profile"] = profileGet;
- UserInfoRequest reqStatus = new UserInfoRequest { users = msisdnToken };
- string rsStatus = UtilsController.SendPost(reqStatus, Session.SessionID, UtilsController.WsType.UsersCheckStatus);
- UserActionResult resStatus = new UserActionResult(rsStatus);
- if (resStatus.status == UtilsController.Constant.SUCCESS)
- {
- UserInfo userInfo = new UserInfo(rsStatus);
- Session["userInfo"] = userInfo;
- Session["Token"] = token;
- // Create new auth
- CreateAuthToken();
- Session["msisdn"] = msisdnToken;
- // Reload user info
- UtilsController.ReloadSubInfo();
- }
- }
- }
- }
- // }
- //}
- }
- return View(model);
- }
- else
- {
- Session["WHICHDEVICE"] = UtilsController.Constant.REGISTER_ON_WEB;
- return View(model);
- }
- return null;
- }
- private string getUserFromToken(string token)
- {
- String resGet = GetUserInfoFromApp(token);
- log.Info(" get user from app result: " + resGet);
- if (resGet != null && resGet != "")
- {
- dynamic json = JsonConvert.DeserializeObject(resGet);
- log.Info("res json: " + json);
- String code = json["code"];
- if (code == "00")
- {
- String msisdnAuto = json["data"]["user"]["phone_number"];
- String userName = json["data"]["user"]["username"];
- log.Info("msisdnAuto: " + msisdnAuto);
- log.Info("userName: " + userName);
- // check phone number
- String msisdn = UtilsController.validateMsisdn(msisdnAuto);
- if (msisdn != null && msisdn != "")
- {
- Session["msisdn"] = msisdn;
- //Session["channel"] = "2";
- //Response.Cookies["channel"].Value = "2";
- //Response.Cookies["token"].Value = token;
- //ServiceLogin.WsGamePortalClient wsClient = new ServiceLogin.WsGamePortalClient();
- //SetWsClient(ref wsClient, Session.SessionID);
- //ServiceLogin.response wsResponse = ReloadAccountInfo(msisdn);
- //if (wsResponse.msisdn != null && wsResponse.msisdn.Length > 0)
- //{
- // CreateAuthToken();
- //}
- return msisdn;
- }
- }
- }
- return null;
- }
- public string GetUserInfoFromApp(String token)
- {
- string res = null;
- try
- {
- ServicePointManager.Expect100Continue = true;
- ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
- HttpClient httpClient = new HttpClient();
- httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
- httpClient.DefaultRequestHeaders.Add("accessToken", ConfigurationManager.AppSettings["accessToken"]);
- log.Info("Req " + ConfigurationManager.AppSettings["pathDetectUser"]);
- HttpResponseMessage response = httpClient.GetAsync(ConfigurationManager.AppSettings["pathDetectUser"]).Result;
- response.EnsureSuccessStatusCode();
- httpClient.Dispose();
- res = response.Content.ReadAsStringAsync().Result;
- }
- catch (Exception ex)
- {
- log.Error(ex);
- }
- return res;
- }
- public ActionResult VietnameseIndex()
- {
- // check login
- try
- {
- if (!CheckAuthToken())
- {
- // show login dialog
- return Redirect("/Home/Index");
- }
- }
- catch (Exception exp)
- {
- System.Diagnostics.Debug.WriteLine("Exception: " + exp);
- }
- Session["COURSE_NOW"] = UtilsController.Constant.VIETNAMESE;
- return View("VietnameseIndex");
- }
- public ActionResult LaosIndex()
- {
- if (!CheckAuthToken())
- {
- // show login dialog
- return Redirect("/Home/Index");
- }
- Session["COURSE_NOW"] = UtilsController.Constant.LAOS;
- return View("LaosIndex");
- }
- public ActionResult EnglishIndex()
- {
- if (!CheckAuthToken())
- {
- // show login dialog
- return Redirect("/Home/Index");
- }
- Session["COURSE_NOW"] = UtilsController.Constant.ENGLISH;
- return View("EnglishIndex");
- }
- public JsonResult CheckAccount(String typeCourse)
- {
- Session["typeLanguage"] = typeCourse;
- if (!CheckAuthToken())
- {
- return Json(new
- {
- code = "-1",
- });
- }
- return Json(new
- {
- code = "0",
- });
- }
- public JsonResult CheckButton(String msisdn)
- {
- String mToken = Session["Token"] as String;
- return Json(new
- {
- token = mToken,
- msisdn = msisdn,
- });
- }
- public JsonResult CourseSet(String courseId)
- {
- Session["COURSE_NOW"] = courseId;
- return Json(new
- {
- code = "0",
- });
- }
- public ActionResult ChangeLanguage(String LanguageAbbrevation)
- {
- if (CheckAuthToken())
- {
- System.Diagnostics.Debug.WriteLine("LanguageAbbrevation: " + LanguageAbbrevation);
- if (LanguageAbbrevation != null)
- {
- Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(LanguageAbbrevation);
- Thread.CurrentThread.CurrentUICulture = new CultureInfo(LanguageAbbrevation);
- }
- //Response.Cookies.Remove("Language");
- HttpCookie cookie = new HttpCookie("Language");
- cookie.Value = LanguageAbbrevation;
- Response.Cookies.Add(cookie);
- return View();
- }
- else
- {
- Session.Clear();
- return Redirect("/Home/Index");
- }
- }
- [ValidateAntiForgeryToken]
- public async Task<JsonResult> RegisterSub(string captcha, string phone, string otp)
- {
- try
- {
- log.Debug("RegisterSub: " + phone);
- String packageName = ConfigurationManager.AppSettings["packname"];
- String fee = ConfigurationManager.AppSettings["fee"];
- // Tạo đối tượng mpsparams với các giá trị từ tham số
- MpsParams mpsparams = new MpsParams
- {
- CMD = "REGISTER",
- PRICE = fee,
- MOBILE = phone,
- CATE = "BLANK",
- SUB = packageName
- };
- // Gọi phương thức bất đồng bộ để đăng ký
- GenKeyWs.response makeUrl = await WapController.UserRegisteringAsync(wsClient, mpsparams);
- // Lưu thông tin vào session
- Session["mps-params"] = mpsparams;
- log.Debug("mpsparams_RegisterSub: " + mpsparams);
- log.Debug("makeUrl_RegisterSub: " + makeUrl);
- // Kiểm tra mã lỗi và trả về kết quả tương ứng
- if (makeUrl.errorCode == UtilsController.WapConstant.SUCCESS)
- {
- return Json(new
- {
- success = true,
- redirectUrl = makeUrl.enscrypt
- });
- }
- else
- {
- return Json(new
- {
- success = false,
- error_code = makeUrl.errorCode,
- error_content = UtilsController.GetErrorCodeCharging(makeUrl.errorCode)
- });
- }
- }
- catch (Exception ex)
- {
- // Log lỗi nếu cần
- return Json(new
- {
- success = false,
- error_code = "UNKNOWN_ERROR",
- error_content = "An unexpected error occurred: "
- });
- }
- }
- //[ValidateAntiForgeryToken]
- //public async Task<ActionResult> RegisterSub(string captcha, string phone, string otp)
- //{
- // try
- // {
- // string packageName = ConfigurationManager.AppSettings["packname"];
- // string fee = ConfigurationManager.AppSettings["fee"];
- // // Tạo đối tượng mpsparams với các giá trị từ tham số
- // MpsParams mpsparams = new MpsParams
- // {
- // CMD = "REGISTER",
- // PRICE = fee,
- // MOBILE = phone,
- // CATE = "BLANK",
- // SUB = packageName
- // };
- // // Gọi phương thức bất đồng bộ để đăng ký
- // GenKeyWs.response makeUrl = await WapController.UserRegisteringAsync(wsClient, mpsparams);
- // // Lưu thông tin vào session
- // Session["mps-params"] = mpsparams;
- // // Kiểm tra mã lỗi và trả về kết quả tương ứng
- // if (makeUrl.errorCode == UtilsController.WapConstant.SUCCESS)
- // {
- // return Redirect(makeUrl.enscrypt); // Điều hướng đến URL thành công
- // }
- // else
- // {
- // // Nếu không thành công, trả về một lỗi hoặc thông báo
- // return Json(new
- // {
- // success = false,
- // error_code = makeUrl.errorCode,
- // error_content = UtilsController.GetErrorCodeCharging(makeUrl.errorCode)
- // }, JsonRequestBehavior.AllowGet);
- // }
- // }
- // catch (Exception ex)
- // {
- // // Log lỗi nếu cần
- // return Json(new
- // {
- // success = false,
- // error_code = "UNKNOWN_ERROR",
- // error_content = "An unexpected error occurred."
- // }, JsonRequestBehavior.AllowGet);
- // }
- //}
- [ValidateAntiForgeryToken]
- public JsonResult RegisterSubWithoutCaptcha()
- {
- if (!CheckAuthToken())
- {
- return Json(new
- {
- error_code = UtilsController.Constant.EXCEPTION,
- error_content = ErrCode.SystemErr
- });
- }
- String msisdn = Session["msisdn"] as string;
- String whichDevice = Session["WHICHDEVICE"] as String;
- if (msisdn != "" && msisdn != null)
- {
- // get package
- String packageId = Session["COURSE_NOW"] as String;
- String package = UtilsController.GetPackagename(packageId, whichDevice, "REGISTER");
- // check account existed
- var checkSub = UtilsController.checkAccountSub(msisdn, package);
- if (checkSub == false)
- {
- //AbandonSession();
- UserRequest userRequest = new UserRequest();
- userRequest.users = msisdn;
- userRequest.msisdn = msisdn;
- userRequest.subServiceCode = package;
- userRequest.requestId = "-1";
- userRequest.otp = "-1";
- String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.MpsRequest);
- UserActionResult res = new UserActionResult(rs);
- if (res.responseCode == UtilsController.Constant.WAITING_OTP)
- //if (res.responseCode == UtilsController.Constant.SUCCESS) // for test
- {
- Charging charging = new Charging(rs);
- Session["charging"] = charging;
- Session["msisdn"] = msisdn;
- Session["package"] = package;
- Session["CHARGE_ACTION"] = UtilsController.Constant.REGISTER;
- Session["CHARGE_DATA"] = userRequest;
- }
- return Json(new
- {
- error_code = res.responseCode,
- error_content = UtilsController.GetErrorCodeCharging(res.responseCode)
- });
- }
- else
- {
- return Json(new
- {
- error_code = UtilsController.Constant.USER_EXISTED,
- error_content = UtilsController.GetErrorCodeCharging(UtilsController.Constant.USER_EXISTED)
- });
- }
- }
- else
- {
- return Json(new
- {
- error_code = UtilsController.Constant.INVALID_MSISDN,
- error_content = Lang.InvalidMsisdn,
- });
- }
- }
- [ValidateAntiForgeryToken]
- public JsonResult ResentOtp()
- {
- String msisdn = Session["msisdn"] as string;
- if (msisdn != "" && msisdn != null)
- {
- String chargeAction = Session["CHARGE_ACTION"] as String;
- UserRequest chargerequest = Session["CHARGE_DATA"] as UserRequest;
- if (chargeAction == UtilsController.Constant.REGISTER)
- {
- // get package
- String whichDevice = Session["WHICHDEVICE"] as String;
- String packageId = Session["COURSE_NOW"] as String;
- String package = UtilsController.GetPackagename(packageId, whichDevice, "REGISTER");
- String rs = UtilsController.SendPost(chargerequest, Session.SessionID, UtilsController.WsType.MpsRequest);
- UserActionResult res = new UserActionResult(rs);
- if (res.responseCode == UtilsController.Constant.WAITING_OTP)
- //if (res.responseCode == UtilsController.Constant.SUCCESS) // for test
- {
- Charging charging = new Charging(rs);
- Session["charging"] = charging;
- Session["msisdn"] = msisdn;
- Session["package"] = package;
- }
- return Json(new
- {
- error_code = res.responseCode,
- error_content = Lang.otpSentAgain
- });
- }
- else
- {
- String rs = UtilsController.SendPost(chargerequest, Session.SessionID, UtilsController.WsType.buyCourse);
- UserActionResult res = new UserActionResult(rs);
- System.Diagnostics.Debug.WriteLine(rs);
- if (res.status == UtilsController.Constant.WAITING_OTP)
- //if (res.status == UtilsController.Constant.SUCCESS) // for test
- {
- Charging charging = new Charging(rs);
- Session["charging"] = charging;
- }
- return Json(new
- {
- error_code = res.status,
- error_content = UtilsController.GetErrorCodeCharging(res.status),
- });
- }
- }
- else
- {
- return Json(new
- {
- error_code = UtilsController.Constant.EXCEPTION,
- });
- }
- }
- [HttpPost]
- public ActionResult UploadImage(HttpPostedFileBase file)
- {
- if (CheckAuthToken())
- {
- System.Diagnostics.Debug.WriteLine("file: " + file);
- // check type of file
- if (file != null && file.ContentLength > 0)
- try
- {
- var supportedTypes = new[] { "jpg", "png" };
- var fileExt = Path.GetExtension(file.FileName).Substring(1);
- System.Diagnostics.Debug.WriteLine("fileExt: " + fileExt);
- System.Diagnostics.Debug.WriteLine("file.ContentLength: " + file.ContentLength);
- if (!supportedTypes.Contains(fileExt))
- {
- //return Json(new
- //{
- // error_content = "File Extension Is InValid - Only Upload WORD/PDF/EXCEL/TXT File",
- //});
- ViewBag.Message = "File Extension Is InValid - Only Upload WORD/PDF/EXCEL/TXT File";
- return View("ProfileView");
- }
- else if (file.ContentLength > (5 * 1024 * 1024))
- {
- //return Json(new
- //{
- // error_content = "File size Should Be UpTo " + 5 + "MB",
- //});
- ViewBag.Message = "File size Should Be UpTo 5MB";
- return View("ProfileView");
- }
- else
- {
- //string path = Path.Combine(Server.MapPath("~/Images"),
- // Path.GetFileName(file.FileName));
- var msisdn = Session["msisdn"] as string;
- System.Diagnostics.Debug.WriteLine("msisdn: " + msisdn);
- if (msisdn != null)
- {
- System.Diagnostics.Debug.WriteLine("FileName: " + Path.GetFileName(file.FileName));
- var fileName = msisdn + "_" + Path.GetFileName(file.FileName);
- var path = Path.Combine(Server.MapPath("~/Content/assets/imgs/post/"), fileName);
- System.Diagnostics.Debug.WriteLine("path: " + path);
- file.SaveAs(path);
- ViewBag.Message = "File uploaded successfully";
- ProfileModel model = new ProfileModel();
- UserProfile userProfile = Session["profile"] as UserProfile;
- // update icon
- userProfile.picture = UtilsController.GetContentPath.IndividualContentPath + fileName;
- String rs = UtilsController.SendPost(userProfile, Session.SessionID, UtilsController.WsType.UsersUpdateProfile);
- UserActionResult res = new UserActionResult(rs);
- if (res.status == UtilsController.Constant.SUCCESS)
- {
- // update success --> store session
- Session["profile"] = userProfile;
- }
- else
- {
- return Redirect("/Common/Error");
- }
- model.profileDetail = userProfile;
- return View("ProfileView", model);
- }
- else
- {
- ViewBag.Message = "File uploaded failure";
- return Redirect("/Common/Error");
- }
- }
- }
- catch (Exception ex)
- {
- ViewBag.Message = "ERROR:" + ex.Message.ToString();
- return Redirect("/Common/Error");
- }
- else
- {
- ViewBag.Message = "You have not specified a file.";
- return Redirect("/Common/Error");
- }
- }
- else
- {
- Session.Clear();
- return Redirect("/Home/Index");
- }
- }
- [HttpPost]
- public ActionResult UploadExistedImage(String picture)
- {
- if (CheckAuthToken())
- {
- // check type of file
- if (picture != null)
- {
- UserProfile userProfile = Session["profile"] as UserProfile;
- // update icon
- userProfile.picture = picture;
- String rs = UtilsController.SendPost(userProfile, Session.SessionID, UtilsController.WsType.UsersUpdateProfile);
- UserActionResult res = new UserActionResult(rs);
- if (res.status == UtilsController.Constant.SUCCESS)
- {
- // update success --> store session
- Session["profile"] = userProfile;
- }
- return Json(new
- {
- error_code = res.status,
- error_content = UtilsController.GetErrorCodeCharging(res.status),
- });
- }
- else
- {
- ViewBag.Message = "You have not specified a file.";
- return Redirect("/Common/Error");
- }
- }
- else
- {
- Session.Clear();
- return Redirect("/Home/Index");
- }
- }
- [ValidateAntiForgeryToken]
- public JsonResult CreateFree(String captcha, String phone)
- {
- try
- {
- if (!ReCaptcha.Validate(captcha))
- {
- return Json(new
- {
- error_code = UtilsController.Constant.USER_EXISTED,
- error_content = "Your captcha is not valid"
- });
- }
- String msisdn = UtilsController.validateMsisdn(phone);
- if (msisdn != "")
- {
- //AbandonSession();
- UserRequest userRequest = new UserRequest();
- userRequest.users = msisdn;
- userRequest.msisdn = msisdn;
- userRequest.command = "REGIST";
- userRequest.channel = "WEB";
- String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.UsersRegister);
- System.Diagnostics.Debug.WriteLine("res: " + rs);
- UserActionResult res = new UserActionResult(rs);
- Session["isSub"] = "false";
- return Json(new
- {
- error_code = res.status,
- error_content = UtilsController.GetErrorCodeCharging(res.status),
- href = res.status == UtilsController.Constant.SUCCESS ? "/Home/Login" : null,
- //href = res.status == UtilsController.Constant.SUCCESS ? "/Home/Login" : null // for test
- });
- }
- else
- {
- return Json(new
- {
- error_code = UtilsController.Constant.INVALID_MSISDN,
- error_content = Lang.InvalidMsisdn,
- });
- }
- }
- catch (Exception ex)
- {
- return Json(new
- {
- error_code = UtilsController.Constant.EXCEPTION,
- error_content = UtilsController.GetErrorCodeCharging("-10"),
- });
- }
- }
- public ActionResult LoginSuccess(String code)
- {
- try
- {
- // verify
- String tempCode = code;
- log.Info("Login success: code = " + code + ", tempCode = " + tempCode);
- Verify.VerifyReq req = new Verify.VerifyReq();
- req.code = code;
- req.secretKey = ConfigurationManager.AppSettings["secretKey"];
- string url = ConfigurationManager.AppSettings["uniIdVerifier"];
- String result = UtilsController.SendPostWithAuthen(req, url, ConfigurationManager.AppSettings["secretKey"]);
- Verify.VerifyRes res = Verify.VerifyRes.Parse(result);
- if (res.status == 100)
- {
- // success -> check token
- String token = res.data.accessToken;
- Dictionary<String, String> headers = new Dictionary<string, string>();
- headers.Add("Authorization", "Bearer " + token);
- result = UtilsController.SendGetWithAuthen(ConfigurationManager.AppSettings["uniIdUsers"], token);
- Verify.UserRes users = Verify.UserRes.Parse(result);
- if (users.status == 100)
- {
- // success
- // login success --> store session
- String msisdn = UtilsController.validateMsisdn(users.data.phone);
- // create new auth
- CreateAuthToken();
- String sessionId = Session.SessionID;
- // login success --> store session
- Session["msisdn"] = msisdn;
- // get profile
- UtilsController.ReloadProfileInfo();
- UserProfile profile = Session["profile"] as UserProfile;
- if (profile != null)
- {
- // get sub
- UtilsController.ReloadSubInfo();
- }
- else
- {
- // create account
- UserRequest userRequest = new UserRequest();
- userRequest.users = msisdn;
- userRequest.msisdn = msisdn;
- userRequest.command = "REGIST";
- userRequest.channel = "WEB";
- String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.UsersRegister);
- System.Diagnostics.Debug.WriteLine("res: " + rs);
- UtilsController.ReloadSubInfo();
- }
- }
- }
- return Redirect("/Home");
- }
- catch (Exception ex)
- {
- log.Error("Exception login success: " + ex.Message, ex);
- return Redirect("/Error");
- }
- }
- [ValidateAntiForgeryToken]
- public JsonResult Login(String phoneNumber, String password, String typeLang)
- {
- try
- {
- String msisdn = UtilsController.validateMsisdn(phoneNumber);
- if (msisdn != "" && msisdn != null)
- {
- //AbandonSession();
- UserInfoRequest user = new UserInfoRequest();
- user.users = msisdn;
- user.msisdn = msisdn;
- user.pass = password;
- user.command = "LOGIN";
- user.channel = "WEB";
- String rs = UtilsController.SendPost(user, Session.SessionID, UtilsController.WsType.UsersRegister);
- UserActionResult res = new UserActionResult(rs);
- // fake
- if (msisdn == "8562098376035")
- {
- // create new auth
- CreateAuthToken();
- String sessionId = Session.SessionID;
- // login success --> store session
- Session["msisdn"] = msisdn;
- // get profile
- UtilsController.ReloadProfileInfo();
- // get sub
- UtilsController.ReloadSubInfo();
- // all info of user was loaded from server
- //UserProfile profile = Session["profile"] as UserProfile;
- String typeLanguage = Session["typeLanguage"] as String;
- // check sub
- String isSub = Session["isSub"] as String;
- //HttpCookie onApp = new HttpCookie("onapp");
- //onApp["isonapp"] = "1";
- //onApp.Expires = DateTime.Now.AddDays(30);
- //Response.Cookies.Add(onApp);
- return Json(new
- {
- error_code = "0",
- error_content = "Success",
- msisdn = msisdn.Substring(UtilsController.CountryCode.Length),
- href = typeLanguage == UtilsController.Constant.LAOS ? "/Home/LaosIndex" :
- typeLanguage == UtilsController.Constant.VIETNAMESE ? "/Home/VietnameseIndex" :
- typeLanguage == UtilsController.Constant.ENGLISH ? "/Home/EnglishIndex" : "/Home/Index"
- });
- }
- else if (res.status == UtilsController.Constant.SUCCESS)
- {
- // create new auth
- CreateAuthToken();
- String sessionId = Session.SessionID;
- // login success --> store session
- Session["msisdn"] = msisdn;
- // get profile
- UtilsController.ReloadProfileInfo();
- // get sub
- UtilsController.ReloadSubInfo();
- // all info of user was loaded from server
- //UserProfile profile = Session["profile"] as UserProfile;
- String typeLanguage = Session["typeLanguage"] as String;
- //HttpCookie onApp = new HttpCookie("onapp");
- //onApp["isonapp"] = "1";
- //onApp.Expires = DateTime.Now.AddDays(30);
- //Response.Cookies.Add(onApp);
- return Json(new
- {
- error_code = res.status,
- error_content = UtilsController.GetErrorCodeCharging(res.status),
- msisdn = msisdn.Substring(UtilsController.CountryCode.Length),
- href = typeLanguage == UtilsController.Constant.LAOS ? "/Home/LaosIndex" :
- typeLanguage == UtilsController.Constant.VIETNAMESE ? "/Home/VietnameseIndex" :
- typeLanguage == UtilsController.Constant.ENGLISH ? "/Home/EnglishIndex" : "/Home/Index"
- });
- }
- else
- {
- return Json(new
- {
- error_code = res.status,
- //error_content = CommonController.ErrorContent(res.status),
- error_content = UtilsController.GetErrorCodeCharging(res.status),
- msisdn = msisdn.Substring(UtilsController.CountryCode.Length)
- });
- }
- }
- else
- {
- return Json(new
- {
- error_code = UtilsController.Constant.INVALID_MSISDN,
- error_content = Lang.InvalidMsisdn,
- });
- }
- }
- catch (Exception ex)
- {
- log.Error("Exception: ", ex);
- return Json(new
- {
- error_code = UtilsController.Constant.EXCEPTION,
- error_content = UtilsController.GetErrorCodeCharging("-10"),
- });
- }
- }
- public ActionResult About()
- {
- ViewBag.Message = "Your application description page.";
- return View();
- }
- public ActionResult AccountProfile()
- {
- if (CheckAuthToken())
- {
- ProfileModel model = new ProfileModel();
- model.profileDetail = Session["profile"] as UserProfile;
- return View("AccountProfileView", model);
- }
- else
- {
- Session.Clear();
- return Redirect("/Home/Index");
- }
- }
- public ActionResult Profile()
- {
- if (CheckAuthToken())
- {
- ProfileModel model = new ProfileModel();
- model.profileDetail = Session["profile"] as UserProfile;
- return View("ProfileView", model);
- }
- else
- {
- Session.Clear();
- return Redirect("/Home/Index");
- }
- }
- public ActionResult ChangePass()
- {
- if (CheckAuthToken())
- {
- ProfileModel model = new ProfileModel();
- model.profileDetail = Session["profile"] as UserProfile;
- return View("ChangePassView", model);
- }
- else
- {
- Session.Clear();
- return Redirect("/Home/Index");
- }
- }
- [ValidateAntiForgeryToken]
- public JsonResult UpdateProfile(String fullname, String birthday)
- {
- try
- {
- if (CheckAuthToken())
- {
- UserProfile userProfile = Session["profile"] as UserProfile;
- userProfile.fullName = fullname;
- userProfile.birthday = birthday;
- String rs = UtilsController.SendPost(userProfile, Session.SessionID, UtilsController.WsType.UsersUpdateProfile);
- UserActionResult res = new UserActionResult(rs);
- if (res.status == UtilsController.Constant.SUCCESS)
- {
- // update success --> store session
- Session["profile"] = userProfile;
- return Json(new
- {
- error_code = res.status,
- error_content = res.message,
- href = "/Home/Profile"
- });
- }
- else
- {
- return Json(new
- {
- error_code = res.status,
- error_content = UtilsController.GetErrorCodeCharging(res.status),
- });
- }
- }
- else
- {
- Session.Clear();
- return Json(new
- {
- error_code = UtilsController.Constant.EXCEPTION,
- error_content = UtilsController.GetErrorCodeCharging("-10"),
- });
- }
- }
- catch (Exception ex)
- {
- return Json(new
- {
- error_code = UtilsController.Constant.EXCEPTION,
- error_content = UtilsController.GetErrorCodeCharging("-10"),
- });
- }
- }
- [ValidateAntiForgeryToken]
- public JsonResult UpdatePass(String oldPassword, String password)
- {
- try
- {
- if (CheckAuthToken())
- {
- UserInfoRequest userRequest = new UserInfoRequest();
- userRequest.users = Session["msisdn"] as string;
- userRequest.passnew = password;
- userRequest.pass = oldPassword;
- userRequest.command = "CHANGEPASS";
- String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.UsersRegister);
- UserActionResult res = new UserActionResult(rs);
- if (res.status == UtilsController.Constant.SUCCESS)
- {
- // update success --> store session
- return Json(new
- {
- error_code = res.status,
- error_content = res.message,
- href = "/Home/Profile"
- });
- }
- else
- {
- return Json(new
- {
- error_code = res.status,
- error_content = UtilsController.GetErrorCodeCharging(res.status),
- });
- }
- }
- else
- {
- Session.Clear();
- return Json(new
- {
- error_code = UtilsController.Constant.EXCEPTION,
- error_content = UtilsController.GetErrorCodeCharging("-10"),
- });
- }
- }
- catch (Exception ex)
- {
- return Json(new
- {
- error_code = UtilsController.Constant.EXCEPTION,
- error_content = UtilsController.GetErrorCodeCharging("-10"),
- });
- }
- }
- [ValidateAntiForgeryToken]
- public JsonResult ResetPassword(String phoneNumber)
- {
- //if (CheckAuthToken())
- //{
- String msisdn = UtilsController.validateMsisdn(phoneNumber);
- if (msisdn != "")
- {
- UserInfoRequest userRequest = new UserInfoRequest();
- userRequest.users = msisdn;
- userRequest.msisdn = msisdn;
- userRequest.command = "RESETPASS";
- String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.UsersRegister);
- UserActionResult res = new UserActionResult(rs);
- if (res.status == UtilsController.Constant.SUCCESS)
- {
- // update success --> store session
- return Json(new
- {
- error_code = res.status,
- error_content = UtilsController.GetErrorCodeCharging(res.status),
- //href = "/Home/Profile"
- });
- }
- else
- {
- return Json(new
- {
- error_code = res.status,
- error_content = UtilsController.GetErrorCodeCharging(res.status),
- });
- }
- }
- else
- {
- return Json(new
- {
- error_code = UtilsController.Constant.INVALID_MSISDN,
- error_content = Lang.InvalidMsisdn,
- });
- }
- }
- [ValidateAntiForgeryToken]
- public JsonResult BuyCourse(String courseId, String phone, int courseChoose, String otpConfirm)
- {
- try
- {
- if (CheckAuthToken())
- {
- if (otpConfirm == null)
- {
- UserRequest userRequest = new UserRequest();
- userRequest.users = phone;
- userRequest.courseId = courseId;
- userRequest.codeBuy = courseChoose == 7 ? "EDU_CHARGE_50" : "EDU_CHARGE_200";
- userRequest.requestId = "-1";
- userRequest.otp = "-1";
- String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.buyCourse);
- UserActionResult res = new UserActionResult(rs);
- System.Diagnostics.Debug.WriteLine(rs);
- if (res.status == UtilsController.Constant.WAITING_OTP)
- //if (res.status == UtilsController.Constant.SUCCESS) // for test
- {
- Charging charging = new Charging(rs);
- Session["charging"] = charging;
- Session["CHARGE_ACTION"] = UtilsController.Constant.BUY_COURSE;
- Session["CHARGE_DATA"] = userRequest;
- }
- return Json(new
- {
- error_code = res.status,
- error_content = UtilsController.GetErrorCodeCharging(res.status),
- });
- }
- else
- {
- String msisdn = UtilsController.validateMsisdn(phone);
- if (msisdn != "")
- {
- UserRequest userRequest = new UserRequest();
- userRequest.users = phone;
- userRequest.courseId = courseId;
- userRequest.codeBuy = courseChoose == 7 ? "EDU_CHARGE_50" : "EDU_CHARGE_200";
- // get requestId on buying course
- Charging charging = Session["charging"] as Charging;
- userRequest.requestId = charging.requestId;
- userRequest.otp = otpConfirm.ToString();
- String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.buyCourse);
- UserActionResult res = new UserActionResult(rs);
- System.Diagnostics.Debug.WriteLine(rs);
- return Json(new
- {
- error_code = res.status,
- error_content = UtilsController.GetErrorCodeCharging(res.status),
- });
- }
- else
- {
- return Json(new
- {
- error_code = UtilsController.Constant.INVALID_MSISDN,
- error_content = Lang.InvalidMsisdn,
- });
- }
- }
- }
- else
- {
- Session.Clear();
- return Json(new
- {
- error_code = UtilsController.Constant.EXCEPTION,
- error_content = UtilsController.GetErrorCodeCharging("-10"),
- });
- }
- }
- catch (Exception ex)
- {
- return Json(new
- {
- error_code = UtilsController.Constant.EXCEPTION,
- error_content = UtilsController.GetErrorCodeCharging("-10"),
- });
- }
- }
- public ActionResult GetRanking()
- {
- if (CheckAuthToken())
- {
- RankingModel model = new RankingModel();
- UserInfoRequest user = new UserInfoRequest();
- user.users = Session["msisdn"] as string;
- user.msisdn = Session["msisdn"] as String;
- String rs = UtilsController.SendPost(user, Session.SessionID, UtilsController.WsType.GetRanking);
- System.Diagnostics.Debug.WriteLine("rs: " + rs);
- UserActionResult res = new UserActionResult(rs);
- if (res.status == UtilsController.Constant.SUCCESS)
- {
- Ranking ranking = new Ranking(rs);
- model.ranking = ranking;
- // get current ranking
- for (int i = 0; i < ranking.listRanking.Count; i++)
- {
- if (ranking.listRanking[i].users == Session["msisdn"] as string)
- {
- model.currentRanking = ranking.listRanking[i];
- }
- }
- return PartialView("_ModalRanking", model);
- }
- else
- {
- return Redirect("/Common/Error");
- }
- }
- else
- {
- Session.Clear();
- return Redirect("/Home/Index");
- }
- }
- private void CreateAuthToken()
- {
- // create session authen
- SessionIDManager manager = new SessionIDManager();
- string newSessionId = manager.CreateSessionID(System.Web.HttpContext.Current);
- Response.Cookies["AuthToken"].Value = newSessionId;
- Session["AuthToken"] = newSessionId;
- }
- private bool CheckAuthToken()
- {
- if (Session["AuthToken"] != null && Request.Cookies["AuthToken"] != null)
- {
- if (!Session["AuthToken"].ToString().Equals(Request.Cookies["AuthToken"].Value))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- else
- {
- return false;
- }
- }
- public RedirectResult Logout()
- {
- Session.Clear();
- return Redirect("/Home/Index");
- }
- }
- }
|