Esempio n. 1
0
        public async Task<ActionResult> Register(RegisterCustomerViewModel model)
        {
            //Verify if the email address is in use on the system
            var emailValid = (from u in db.Users
                              where u.Email == model.Email
                              select u).Count();

            //If the email is already in use the user receives an error message
            if (emailValid > 0)
            {
                ViewBag.StatusMessage = "Email already exists.";
            }

            //Verify if the model is valid
            //if the model is valid then proceed
            if (ModelState.IsValid)
            {
                //Generate activation code and store it in the variable activationCode
                var activationCode = CodeGenerator();

                //Create a new instance of CustomerViewModel and store it in object customer
                var customer = new CustomerViewModel()
                {
                    UserName = model.Email,
                    Email = model.Email,

                    //Assign value of activationCode to the property ActivationCode of customer
                    ActivationCode = activationCode,
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    Name = model.FirstName + " " + model.LastName,
                    Status = "Pending",
                    DtCreated = DateTime.UtcNow,
                    PhoneNumber = model.PhoneNumber
                };

                //Async method to create a new customer
                var result = await UserManager.CreateAsync(customer, model.Password);

                //If the method property succeeds
                if (result.Succeeded)
                {
                    //Signin the new customer in the application
                    await SignInManager.SignInAsync(customer, isPersistent: false, rememberBrowser: false);

                    //Create an instance of DeliveryAddressViewModel and store it variable customerAddress
                    var customerAddress = new DeliveryAddressViewModel()
                    {
                        Street = model.Street,
                        City = model.City,
                        UserId = customer.Id,
                        CustomerId = customer.CustomerId,
                        Zipcode = model.ZipCode,
                        Status = "Default"
                    };
                    db.DeliveryAddresses.Add(customerAddress);

                    //Save changes into the database
                    db.SaveChanges();

                    //It the body of the email that will be sent to the user after the registration process
                    var body = "<p>Dear Valued Customer,</p><p>This is the activation code that has been sent to you in order to validate your registration on BontoBuy</p>" +
                        "<p>Your activation code: {0}</p>";

                    var message = new MailMessage();

                    //It contains the recipient of the email
                    message.To.Add(new MailAddress(model.Email));

                    //It contains the email address of BontoBuy
                    message.From = new MailAddress("*****@*****.**");

                    //Subject of the mail
                    message.Subject = "Register on BontoBuy";

                    //Using formatted string the activation code is then added to the body of the email
                    message.Body = string.Format(body, activationCode);
                    message.IsBodyHtml = true;

                    var smtp = new SmtpClient();

                    //Use credential of BontoBuy email
                    var credential = new NetworkCredential()
                    {
                        UserName = "******",
                        Password = "******"
                    };
                    smtp.Credentials = credential;
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.EnableSsl = true;
                    await smtp.SendMailAsync(message);

                    ApplicationUser currentUser = db.Users.Where(u => u.Email.Equals(model.Email, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
                    if (customer != null) UserManager.AddToRole(customer.Id, "Customer");

                    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                    return RedirectToAction("RegistrationLogin", "Account");
                }

                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Esempio n. 2
0
        private AdminUpdateCustomerRoleViewModel AssignCustomerDetailsForUpdate(CustomerViewModel record)
        {
            if (record == null || String.IsNullOrWhiteSpace(record.Id))
                return null;

            var itemToUpdate = new AdminUpdateCustomerRoleViewModel()
            {
                UserId = record.Id,
                Email = record.Email,
                Status = record.Status,
                UserRoles = db.Roles.OrderBy(x => x.Name).ToList()
            };

            if (itemToUpdate == null || String.IsNullOrWhiteSpace(itemToUpdate.UserId))
                return null;

            return itemToUpdate;
        }