Esempio n. 1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            var check = new CheckAccountViewModel()
            {
                Email       = model.Email,
                PhoneNumber = model.PhoneNumber,
                UserName    = model.UserName
            };

            if (ModelState.IsValid && (await _userService.FindUserByEmailOrUserNameOrPhoneNumber(check)) == null)
            {
                var messageFree = await _configService.GetConfigByCodeAsync(Common.Constants.MessageFreeKey);

                var user = new AppUser
                {
                    UserName             = model.UserName,
                    Email                = model.Email,
                    FullName             = model.FullName,
                    PhoneNumber          = model.PhoneNumber,
                    BirthDay             = model.BirthDay,
                    Address              = string.Empty,
                    Status               = Status.Active,
                    Gender               = model.Gender,
                    Avatar               = Common.Constants.AvatarDefault,
                    DateCreated          = DateTime.Now,
                    DateModified         = DateTime.Now,
                    PhoneNumberConfirmed = true,
                    Balance              = 0,
                    TotalFreeMessage     = (messageFree != null && messageFree.ValueNumber.HasValue) ? (int)messageFree.ValueNumber :  Common.Constants.MessageFreeDefault
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    await _emailService.SendEmailAsync(model.Email, "Confirm your account",
                                                       "Thank you for registing to our SMS Online service.<br> Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 2
0
        public ActionResult Register()
        {
            bool isAuthenticated = (System.Web.HttpContext.Current.User != null) &&
                                   System.Web.HttpContext.Current.User.Identity.IsAuthenticated;

            if (isAuthenticated)
            {
                return(RedirectToAction("Index", "Home"));
            }
            RegisterViewModel model = new RegisterViewModel();

            return(View(model));
        }