Exemplo n.º 1
0
 public IActionResult Register(Customer customer)
 {
     if (ModelState.IsValid)
     {
         repository.AddCustomer(customer);
     }
     return(RedirectToAction("Index", "Home"));
 }
Exemplo n.º 2
0
        public async Task <IActionResult> Register(CustomerWithPassword customerWithPassword)
        {
            if (ModelState.IsValid)
            {
                Customers customer = customerWithPassword.Customer;
                if (repository.Customers.Any(c => c.CompanyName == customer.CompanyName))
                {
                    ModelState.AddModelError("", "Company Name must be unique");
                }
                else
                {
                    if (ModelState.IsValid)
                    {
                        AppUser user = new AppUser
                        {
                            // email and username are synced - this is by choice
                            Email    = customer.Email,
                            UserName = customer.Email
                        };
                        // Add user to Identity DB
                        IdentityResult result = await userManager.CreateAsync(user, customerWithPassword.Password);

                        if (!result.Succeeded)
                        {
                            AddErrorsFromResult(result);
                        }
                        else
                        {
                            // Assign user to customers Role
                            result = await userManager.AddToRoleAsync(user, "Customer");

                            if (!result.Succeeded)
                            {
                                // Delete User from Identity DB
                                await userManager.DeleteAsync(user);

                                AddErrorsFromResult(result);
                            }
                            else
                            {
                                // Create customer (Northwind)
                                repository.AddCustomer(customer);
                                return(RedirectToAction("Index", "Home"));
                            }
                        }
                    }
                }
            }
            return(View());
        }
Exemplo n.º 3
0
        public IActionResult AddCustomer(Customer customer)
        {
            //Add more validation here I guess
            //And maybe redirect to an error page?
            if (string.IsNullOrWhiteSpace(customer.CompanyName) ||
                _db.Customers.Any(c => c.CompanyName == customer.CompanyName))
            {
                return(View(customer));
            }

            //Do the database thing
            _db.AddCustomer(customer);
            return(View(customer));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Register(CustomerWithPassword customerWithPassword)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }
            var customer = customerWithPassword.Customer;

            if (repository.Customers.Any(c => c.CompanyName == customer.CompanyName))
            {
                ModelState.AddModelError("", "The company name must be unique!");
            }
            else
            {
                if (ModelState.IsValid)
                {
                    var user = new AppUser
                    {
                        Email    = customer.Email,
                        UserName = customer.Email
                    };

                    var result = await userManager.CreateAsync(user, customerWithPassword.Password);

                    if (!result.Succeeded)
                    {
                        AddErrorsFromResult(result);
                    }
                    else
                    {
                        result = await userManager.AddToRoleAsync(user, "Customer");

                        if (!result.Succeeded)
                        {
                            await userManager.DeleteAsync(user);

                            AddErrorsFromResult(result);
                        }
                        else
                        {
                            repository.AddCustomer(customer);
                            return(RedirectToAction("Index", "Home"));
                        }
                    }
                }
            }

            return(View());
        }
Exemplo n.º 5
0
        public async System.Threading.Tasks.Task <IActionResult> Register(CustomerWithPassword customerWithPassword)
        {
            if (ModelState.IsValid)
            {
                Customer customer = customerWithPassword.Customer;
                if (ModelState.IsValid)
                {
                    AppUser user = new AppUser
                    {
                        // email and username are synced - this is by choice
                        Email    = customer.Email,
                        UserName = customer.Email
                    };
                    // Add user to Identity DB
                    IdentityResult result = await userManager.CreateAsync(user, customerWithPassword.Password);

                    if (!result.Succeeded)
                    {
                        AddErrorsFromResult(result);
                    }
                    else
                    {
                        // Assign user to customers Role
                        result = await userManager.AddToRoleAsync(user, "Customer");

                        if (!result.Succeeded)
                        {
                            // Delete User from Identity DB
                            await userManager.DeleteAsync(user);

                            AddErrorsFromResult(result);
                        }
                        else
                        {
                            // Send email
                            logger.LogDebug("User created a new account with password.");

                            logger.LogDebug("Sending email confirmation.");
                            var code = await userManager.GenerateEmailConfirmationTokenAsync(user);

                            code = WebEncoders.Base64UrlEncode(System.Text.Encoding.UTF8.GetBytes(code));

                            var callbackUrl = Request.Scheme + "://" +
                                              Request.Host.Value +
                                              "/Account/ConfirmEmail?" +
                                              "userId=" + user.Id +
                                              "&code=" + code;

                            await emailSender.SendEmailAsync(customer.Email, "Confirm your email",
                                                             $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                            // End Send Email

                            // Create customer (Northwind)
                            logger.LogDebug("Adding user to database.");
                            repository.AddCustomer(customer);
                            return(RedirectToAction("Index", "Home"));
                        }
                    }
                }
            }
            return(View());
        }