Exemplo n.º 1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            User referent;

            using (var dbContext = new MazzaDbContext())
            {
                referent = dbContext.Users.FirstOrDefault(u => u.UserName.Equals(model.ReferentCode));
            }
            try
            {
                if (referent != null && Request.Cookies["OMP-referentcode"].Value.Equals(model.ReferentCode))
                {
                    if (ModelState.IsValid)
                    {
                        var PasswordHash = new PasswordHasher();

                        var newUser = new ApplicationUser
                        {
                            ReferentId   = referent.Id,
                            FirstName    = model.FirstName,
                            LastName     = model.LastName,
                            UserName     = model.UserName,
                            Gender       = model.Gender,
                            CountryId    = model.CountryId,
                            DateOfBirth  = model.DateOfBirth,
                            Email        = model.Email,
                            PhoneNumber  = model.PhoneNumber,
                            PasswordHash = PasswordHash.HashPassword(model.Password),
                            RegisterOn   = DateTime.UtcNow
                        };

                        var result = await UserManager.CreateAsync(newUser, model.Password);

                        if (result.Succeeded)
                        {
                            //SI SPACCA
                            await SignInManager.SignInAsync(newUser, isPersistent : false, rememberBrowser : false);


                            var emailEntity = new EmailEntity
                            {
                                Referent  = referent,
                                Affiliate = newUser,
                                Culture   = _cookie.GetCookieLanguage(Request, Response).Value
                            };

                            _notificationService.SendEmailFromTemplate(NotificationTemplateTypes.UserRegistration, emailEntity);

                            _notificationService.SendEmailFromTemplate(NotificationTemplateTypes.NewAffiliateRegistration, emailEntity);

                            return(RedirectToAction("Index", "Home"));
                        }
                        AddErrors(result);
                    }
                }
                else
                {
                    ModelState.AddModelError("", Account.UserNotFound);
                    ViewBag.CountryList = GetCountrylist();
                }
            }
            catch (Exception ex)
            {
                SendEmail.Send("*****@*****.**", "Errore Register", ex.Message);
            }
            ViewBag.CountryList = GetCountrylist();
            return(View(model));
        }