示例#1
0
        public bool CreateProfile(ApplicationUser user, RegisterViewModel model)
        {
            ThanhVien member = new ThanhVien();
            member.TinhTrang = true;
            member.HoTen = model.Name;
            member.NgaySinh = model.Birthday;

            member.Avatar = "/Images/avatar_default.png";
            member.DiaChi = model.Address;
            member.GioiTinh = model.IsMale;
            member.MS_TaiKhoan = user.Id;
            member = bus.createNewProfile(member);
            if (member != null)
            {
                return true;
            }
            return false;
        }
示例#2
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {

            var response = Request["g-recaptcha-response"];
            //secret that was generated in key value pair
            const string secret = "6LfM3wcTAAAAALCOa3vDbWM7XZyu0EaIa9kD28_z";

            var client = new WebClient();
            var reply =
                client.DownloadString(
                    string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", secret, response));

            var captchaResponse = JsonConvert.DeserializeObject<CaptchaResponse>(reply);

            //when response is false check for the error message
            if (!captchaResponse.Success)
            {
                if (captchaResponse.ErrorCodes.Count <= 0) return View(model);

                var error = captchaResponse.ErrorCodes[0].ToLower();
                switch (error)
                {
                    case ("missing-input-secret"):
                        ViewBag.Message = "The secret parameter is missing.";
                        break;
                    case ("invalid-input-secret"):
                        ViewBag.Message = "The secret parameter is invalid or malformed.";
                        break;

                    case ("missing-input-response"):
                        ViewBag.Message = "The response parameter is missing.";
                        break;
                    case ("invalid-input-response"):
                        ViewBag.Message = "The response parameter is invalid or malformed.";
                        break;

                    default:
                        ViewBag.Message = "Xác nhận Captcha bị lỗi! Vui lòng thử lại!";
                        break;
                }
                return PartialView(model);
            }
            else
            {
                ViewBag.Message = "";
            }

     
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };

                user.DayRegister = DateTime.Today;
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    if (!AC.CreateProfile(user, model))
                    {
                        await UserManager.DeleteAsync(user);
                        ModelState.AddModelError("user_error", "Không thể tạo tài khoản của bạn!");
                        return PartialView(model);
                    }

                    await UserManager.AddToRoleAsync(user.Id, "Member");

                  //  await SignInHelper.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account",
                       new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);

                    await UserManager.SendEmailAsync(user.Id,
                       "Xác nhận tài khoản của bạn", "Cảm ơn bạn đã đăng ký tài khoản trên website của chúng tôi <br/> "+
                       "Vui lòng xác nhận tài khoản bằng cách nhấn vào <a href=\""
                       + callbackUrl + "\">đây</a>");
                    string script = "window.location.href='" + Url.Action("DisplayEmail", "Account") + "'";
                    return JavaScript(script);
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return PartialView(model);
        }