示例#1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            UserManager = new CUserManager(new CUserStore(UnitOfWork));
            if (ModelState.IsValid)
            {
                var user = new CUser()
                {
                    UserName = model.UserName
                };
                var result = await UserManager.CreateAsync(user, model.Password);

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

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
示例#2
0
        public async Task <IActionResult> Creer(Person client)
        {
            if (!ModelState.IsValid)
            {
                return(View(client));
            }

            // Création d'un compte client
            var result = await _userManager.CreateAsync(client);

            if (result.Succeeded)
            {
                // Ajout au rôle de client
                result = await _userManager.AddToRoleAsync(client, GlobalResources.ROLE_CLIENT);

                if (result.Succeeded)
                {
                    // Génération du token pour la génération du mot de passe
                    var Token = await _userManager.GeneratePasswordResetTokenAsync(client);

                    // Envoi du mail pour la création du compte
                    var callbackUrl = Url.Action("ResetPassword", "Login", new { area = "", userId = client.Id, code = Token }, Request.Scheme);

                    await _emailSender.SendEmailAsync(client.Email, "Veuillez définir votre mot de passe",
                                                      $"Veuillez définir votre mot de passe <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>En cliquant ici</a>.");
                }
                else
                {
                    // Retour des erreurs
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError(string.Empty, error.Description);
                    }
                    return(View(client));
                }
            }
            else
            {
                // Retour des erreurs
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
                return(View(client));
            }

            return(RedirectToAction(nameof(Index)));
        }
示例#3
0
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Manage"));
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new CUser()
                {
                    UserName = model.UserName
                };
                var result = await UserManager.CreateAsync(user);

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

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

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }