using LotteryWebApp.Common; using LotteryWebApp.Models; using LotteryWebApp.Service; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using System; using LotteryWebApp.Languages; using LotteryWebApp.Extensions; using System.Globalization; using Microsoft.AspNetCore.Http; using System.IO; namespace LotteryWebApp.Controllers { [AutoValidateAntiforgeryToken] public class AccountController : BaseController { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program)); IConfiguration configuration; private readonly IWebHostEnvironment webHostEnvironment; APIFunctions api = new APIFunctions(); public AccountController(IConfiguration _configuration, IWebHostEnvironment hostEnvironment) { configuration = _configuration; webHostEnvironment = hostEnvironment; } public String GetParameter(String key) { return configuration.GetSection(key).Value; } public IActionResult Index() { if (!CheckAuthToken()) { return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login"); } AccountIndex_ViewModel model = new AccountIndex_ViewModel(); return View("Index", model); } public IActionResult Login(String code, String step, String phonenumber, String message) { //return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Home/Update"); AccountLogin_ViewModel model = new AccountLogin_ViewModel(); try { model.code = code; model.step = step != null ? step : Constants.LOGIN_ENTER_MSISDN; model.message = message; } catch (Exception ex) { log.Error(ex); } return View("Login", model); } public IActionResult Logout() { ClearCache(); return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login"); } [ValidateAntiForgeryToken] public JsonResult CheckMsisdn_Action(String phonenumber) { // check Msisdn String msisdn = validateMsisdn(phonenumber); if (msisdn == "") { return Json(new { code = Code.FAILURE, message = Lang.phone_invalid }); } //msisdn = "50940236545"; HttpContext.Session.SetComplexData("msisdn", msisdn); return Json(new { code = Code.SUCCESS, }); } [ValidateAntiForgeryToken] public JsonResult ForgotPassword_Action(string phonenumber) { try { // resent password to return result String msisdn = HttpContext.Session.GetComplexData("msisdn"); //String msisdn = validateMsisdn(phonenumber); if (msisdn == null) { return Json(new { code = Code.FAILURE, message = Lang.phone_invalid }); } // bo cho test RegisterRequest request = new RegisterRequest(); request.Msisdn = msisdn; RegisterResponse reset = api.UserForgotPasswordApi(configuration, request); return Json(new { code = int.Parse(reset.status), message = GetLangFromCode(reset.status) }); //return Json(new //{ // code = Code.SUCCESS, //}); } catch (Exception ex) { log.Error(ex); } return Json(new { code = Code.FAILURE, message = Lang.error_happened }); } [ValidateAntiForgeryToken] public JsonResult Login_Action(String step, String phonenumber, String password) { try { String msisdn = HttpContext.Session.GetComplexData("msisdn"); if (msisdn != null) { //password = "589219"; RegisterRequest request = new RegisterRequest(); request.Msisdn = msisdn; request.pass = password; RegisterResponse login = api.UserLoginApi(configuration, request); if (login.status == Code.SUCCESS.ToString()) { // create session // create new auth CreateAuthToken(); // login success --> store session HttpContext.Session.SetComplexData("msisdn", msisdn); HttpContext.Session.SetComplexData("token", login.token); // load profile // load user status UserGetProfileRequest userGetProfileRequest = new UserGetProfileRequest { users = msisdn, token = login.token }; Profile profile = api.UserLoadProfileApi(configuration, userGetProfileRequest); HttpContext.Session.SetComplexData("profile", profile); // load profile UserStatusRequest userStatusRequest = new UserStatusRequest { users = msisdn, token = login.token }; UserStatus userStatus = api.GetUserStatusApi(configuration, userStatusRequest); HttpContext.Session.SetComplexData("userStatus", userStatus); } return Json(new { code = int.Parse(login.status), message = GetLangFromCode(login.status) }); } else { return Json(new { code = Code.FAILURE, message = Lang.error_happened }); } } catch (Exception ex) { log.Error(ex); } return Json(new { code = Code.FAILURE, message = Lang.error_happened }); } public IActionResult Register(string code) { AccountRegister_ViewModel model = new AccountRegister_ViewModel(); model.code = code; return View("Register", model); } [ValidateAntiForgeryToken] public JsonResult Register_Action(string phonenumber, string fullname, string birthday) { try { // resent password to return result String msisdn = validateMsisdn(phonenumber); if (msisdn == "") { return Json(new { code = Code.FAILURE, message = Lang.phone_invalid }); } if (fullname == null) { return Json(new { code = Code.FAILURE, message = Lang.fullname_not_valid }); } RegisterRequest request = new RegisterRequest(); request.Msisdn = msisdn; RegisterResponse register = api.UserRegisterApi(configuration, request); if (register.status == Code.SUCCESS) { // convert birthday to dd/mm/yyyy String birthdayFormat = DateTime.ParseExact(birthday, "yyyy-MM-dd", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy"); Profile profile = new Profile(); profile.birthday = birthdayFormat; profile.fullName = fullname; HttpContext.Session.SetComplexData("profile", profile); HttpContext.Session.SetComplexData("token", register.token); // create profile UserUpdateProfileRequest userUpdateProfileRequest = new UserUpdateProfileRequest(); userUpdateProfileRequest.users = msisdn; userUpdateProfileRequest.fullName = fullname; userUpdateProfileRequest.birthday = birthdayFormat; userUpdateProfileRequest.token = register.token; UserUpdateProfileResponse updateProfile = api.UserUpdateProfileApi(configuration, userUpdateProfileRequest); if (updateProfile.status == Code.SUCCESS) { // return login screen } } return Json(new { code = int.Parse(register.status), message = GetLangFromCode(register.status) }); } catch (Exception ex) { log.Error(ex); } return Json(new { code = Code.FAILURE, message = Lang.error_happened }); } public IActionResult ForgotPassword(String code) { if (!CheckAuthToken()) { return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login"); } AccountForgotPassword_ViewModel model = new AccountForgotPassword_ViewModel(); model.code = code; return View("ForgotPassword", model); } [ValidateAntiForgeryToken] [Produces("application/json")] [Consumes("multipart/form-data")] public JsonResult UploadAvatar([FromForm] IFormFile user_avatar) { try { // check user if (!CheckAuthToken()) { log.Info("Must login"); return Json(new { code = Code.NOT_AUTHEN, message = Lang.must_login }); } //var user_avatar = HttpContext.Request.Form.Files; if (user_avatar != null) { String msisdn = HttpContext.Session.GetComplexData("msisdn"); Profile profile = HttpContext.Session.GetComplexData("profile"); IFormFile image = user_avatar; string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "img/" + profile.users); Directory.CreateDirectory(uploadsFolder); string uniqueFileName = Guid.NewGuid().ToString() + "_" + image.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { image.CopyTo(fileStream); } profile.realPicture = uniqueFileName; profile.picture = Constants.PATH + profile.users + "/" + uniqueFileName; HttpContext.Session.SetComplexData("profile", profile); return Json(new { code = Code.SUCCESS, }); } else { log.Error("user avatar error"); return Json(new { code = Code.ERROR, message = Lang.user_avatar_error }); } } catch (Exception ex) { log.Error("Exception ", ex); } return Json(new { code = Code.FAILURE, message = Lang.error_happened }); } } }