Beispiel #1
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            //Aquí tuvimos que preguntar con un if si el rol del usuario es de Tecnico o Client.
            //Si bien esto puede ser considerado como "bad smell", decidimos hacerlo de esta forma ya
            //que la aplicación necesita saber si el usuario que se registra es de tipo Tecnico o Client
            //y de esta forma se instancia como uno de esos tipos y no como ApplicationUser.
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                if (Input.Role == "Tecnico")
                {
                    var user = new Ignis.Areas.Identity.Data.Tecnico {
                        Name           = Input.Name,
                        DOB            = Input.DOB,
                        UserName       = Input.Email,
                        Email          = Input.Email,
                        Role           = Input.Role,
                        AverageRanking = 0,
                        Available      = true,
                        WorkedHours    = 0,
                        TotalWorks     = 0
                    };
                    var result = await _userManager.CreateAsync(user, Input.Password);

                    if (result.Succeeded)
                    {
                        _logger.LogInformation("User created a new account with password.");

                        var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        var callbackUrl = Url.Page(
                            "/Account/ConfirmEmail",
                            pageHandler: null,
                            values: new { userId = user.Id, code = code },
                            protocol: Request.Scheme);

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

                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(LocalRedirect(returnUrl));
                    }

                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError(string.Empty, error.Description);
                    }
                }
                if (Input.Role == "Cliente")
                {
                    var user = new Ignis.Areas.Identity.Data.Client {
                        Name     = Input.Name,
                        DOB      = Input.DOB,
                        UserName = Input.Email,
                        Email    = Input.Email,
                        Role     = Input.Role,
                        Projects = 0,
                    };
                    var result = await _userManager.CreateAsync(user, Input.Password);

                    if (result.Succeeded)
                    {
                        _logger.LogInformation("User created a new account with password.");

                        var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        var callbackUrl = Url.Page(
                            "/Account/ConfirmEmail",
                            pageHandler: null,
                            values: new { userId = user.Id, code = code },
                            protocol: Request.Scheme);

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

                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(LocalRedirect(returnUrl));
                    }

                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError(string.Empty, error.Description);
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }