Exemplo n.º 1
0
        public async Task <ActionResult> Register([FromBody] RegisterViewModel model)
        {
            var commandResult = new CommandResult();

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    //Id=Guid.NewGuid(),
                    Email       = model.Email,
                    UserName    = model.Email,
                    CreatedDate = new DateTime(),
                    FirstName   = StringHelper.FirstLetterToUpper(model.FirstName),
                    LastName    = StringHelper.FirstLetterToUpper(model.LastName)
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    //  Envoyer un message électronique contenant ce lien
                    await SaveUserCommand.SendConfirmEmailAsync(_emailService, UserManager,
                                                                new SendConfirmEmailModel
                    {
                        Email     = user.Email,
                        FirstName = user.FirstName,
                        LastName  = user.LastName
                    }, user);

                    return(new JsonResult(commandResult));
                }

                // TODO utiliser framework validation
                foreach (var error in result.Errors)
                {
                    commandResult.ValidationResult.AddError("Email", error.Code);
                }
            }
            else
            {
                foreach (var key in ModelState.Keys)
                {
                    var value = ModelState[key];
                    if (value.Errors.Count > 0)
                    {
                        commandResult.ValidationResult.AddError(key, value.Errors[0].ErrorMessage);
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return(new JsonResult(commandResult));
        }
Exemplo n.º 2
0
        //  [ValidateAntiForgeryToken]
        public async Task <IActionResult> ExternalLoginConfirmation([FromBody] ExternalLoginConfirmationViewModel model)
        {
            if (User.Identity.IsAuthenticated)
            {
                throw new Exception("Vous êtes déjà authentifié");
            }
            // Obtenez des informations sur l’utilisateur auprès du fournisseur de connexions externe
            var commandResult = new CommandResult();

            var info = await SignInManager.GetExternalLoginInfoAsync();

            if (ModelState.IsValid)
            {
                if (info == null)
                {
                    commandResult.ValidationResult.AddError("EXTERNAL_PROVIDER",
                                                            "Erreur lors de la récupération des informations du compte externe.");
                    return(new JsonResult(commandResult));
                }

                var user = new ApplicationUser
                {
                    //Id= Guid.NewGuid(),
                    Email       = model.Email,
                    UserName    = model.Email,
                    CreatedDate = new DateTime(),
                    FirstName   = StringHelper.FirstLetterToUpper(model.FirstName),
                    LastName    = StringHelper.FirstLetterToUpper(model.LastName)
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user, info);

                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, false);

                        // Pour plus d'informations sur l'activation de la confirmation du compte et la réinitialisation du mot de passe, consultez http://go.microsoft.com/fwlink/?LinkID=320771
                        //  Envoyer un message électronique contenant ce lien
                        await SaveUserCommand.SendConfirmEmailAsync(_emailService, UserManager,
                                                                    new SendConfirmEmailModel
                        {
                            Email     = user.Email,
                            FirstName = user.FirstName,
                            LastName  = user.LastName,
                            Provider  = model.Provider
                        }, user);

                        return(new JsonResult(commandResult));
                    }
                }

                // TODO utiliser framework validation
                foreach (var error in result.Errors)
                {
                    commandResult.ValidationResult.AddError("Email", error.Code);
                }
            }
            else
            {
                foreach (var key in ModelState.Keys)
                {
                    var value = ModelState[key];
                    if (value.Errors.Count > 0)
                    {
                        commandResult.ValidationResult.AddError(key, value.Errors[0].ErrorMessage);
                    }
                }
            }

            return(new JsonResult(commandResult));
        }