Example #1
0
        public async Task <IActionResult> Register(RegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            if (model.AvatarPhoto != null)
            {
                var filePath = Path.GetTempFileName();
                using (var stream = System.IO.File.Create(filePath))
                {
                    await model.AvatarPhoto.CopyToAsync(stream);
                }
                model.AvatarURL   = UploadImageService.Instance().Upload(model.UserName, filePath);
                model.AvatarPhoto = null;
            }
            model.AvatarPhoto = null;
            //string url = model.AvatarPhotoURL;
            var user = await _accountService.Register(model);

            if (user != null)
            {
                if (!string.IsNullOrEmpty(user.msg))
                {
                    ViewData["msg"] = user.msg;
                    return(View(model));
                }
                ViewData["IsRegisterSuccess"] = true;
                return(View(new RegisterModel()));
            }
            ViewData["IsRegisterSuccess"] = false;
            return(View(model));
        }
Example #2
0
        public async Task <IActionResult> OnPostUpdateProfile(int id, UserViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction(nameof(UpdateProfile), new
                {
                    id
                }));
            }
            if (model.AvatarPhoto != null)
            {
                var filePath = Path.GetTempFileName();
                using (var stream = System.IO.File.Create(filePath))
                {
                    await model.AvatarPhoto.CopyToAsync(stream);
                }
                model.AvatarURL   = UploadImageService.Instance().Upload(model.UserName, filePath);
                model.AvatarPhoto = null;
            }
            var userID = Int32.Parse(User.FindFirst("UserID").Value);

            if (id != userID)
            {
                return(Redirect("/Views/Account/AccessDenied.cshtml"));
            }
            var access_token = CookieEncoder.DecodeToken(Request.Cookies["access_token_cookie"]);
            var result       = await _accountService.UpdateProfile(model, access_token);

            // success
            if (result != null && result.data != null)
            {
                return(RedirectToAction(nameof(ShowProfile)));
            }
            // fail
            ViewData["msg"] = "Update failed";
            return(RedirectToAction(nameof(UpdateProfile), new
            {
                id = id
            }));
        }