// POST: PublisherController/Create
        public async Task <ActionResult> AddPublisherAsync(PublisherViewModel model)
        {
            try
            {
                var userData = HttpContext.Session.GetObjectFromJson <LoginPassedDataViewModel>("userCredentials");
                //Save Congregation ID
                var user = new ApplicationUser
                {
                    FKPublisherCongregation = userData.CongregationId,
                    UserName                = model.Email,
                    Email                   = model.Email,
                    PhoneNumber             = model.PhoneNumber,
                    PublisherFirstName      = model.PublisherFirstName,
                    PublisherLastName       = model.PublisherLastName,
                    EnumPublisherSex        = model.EnumPublisherSex,
                    EnumPublisherPrivileges = model.EnumPublisherPrivileges,
                    EnumRecordStatus        = model.EnumRecordStatus
                };
                //Autogenerate Password
                Guid   obj            = Guid.NewGuid();
                char[] characterArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
                var    random         = new Random();
                int    randomNumber   = random.Next(25);
                model.Password = obj.ToString() + characterArray[randomNumber].ToString();
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    ViewBag.ErrorTitle   = "Registration Sucessful";
                    ViewBag.ErrorMessage = "Before you can Login, please confirm your" +
                                           "email, by clicking on the linke that we have emailed you.";
                    return(RedirectToAction("Index", new { id = userData.CongregationId }));
                    //await signInManager.SignInAsync(user, isPersistent: false);
                    //return RedirectToAction("index","home");
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
                //Save to DB

                //Go back to main page
                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
예제 #2
0
        public async Task <IActionResult> RegisterAsync(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName                = model.Email,
                    Email                   = model.Email,
                    PhoneNumber             = model.PhoneNumber,
                    PublisherFirstName      = model.PublisherFirstName,
                    PublisherLastName       = model.PublisherLastName,
                    EnumPublisherSex        = model.PublisherSex,
                    EnumPublisherPrivileges = model.PublisherPrivileges,
                    EnumRecordStatus        = model.PublisherActive
                };
                var result = await userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //Create email confirmation token
                    var token = await userManager.GenerateEmailConfirmationTokenAsync(user);

                    var confirmationLink = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, token = token }, Request.Scheme);
                    await emailSender.SendEmailConfirmationAsync(user, confirmationLink);

                    //log email sent
                    logger.Log(LogLevel.Warning, confirmationLink);

                    ViewBag.ErrorTitle   = "Registration Sucessful";
                    ViewBag.ErrorMessage = "Before you can Login, please confirm your" +
                                           "email, by clicking on the linke that we have emailed you.";
                    return(View("Home"));
                    //await signInManager.SignInAsync(user, isPersistent: false);
                    //return RedirectToAction("index","home");
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }
            return(View(model));
        }