Exemple #1
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var user = new AppUser
                    {
                        UserName  = model.UserName,
                        Email     = model.Email,
                        FirstName = model.FirstName,
                        LastName  = model.LastName
                    };

                    var result = await UserMgr.CreateAsync(user, model.Password);

                    if (result.Succeeded)
                    {
                        var token = await UserMgr.GenerateEmailConfirmationTokenAsync(user);

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

                        logger.Log(LogLevel.Warning, confirmationLink);

                        ViewBag.MessageTitle = "Registro feito com sucesso!";
                        ViewBag.MessageBody  = "Antes que você possa fazer login, por favor, confirme " +
                                               "seu email clicando no link de confirmação que mandamos para seu email";
                        return(View("ConfirmEmail"));
                    }

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

                    return(View());
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);

                    return(View(model));
                }
            }

            return(View(model));
        }