Ejemplo n.º 1
0
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var user = await userManager.FindByNameAsync(model.Email);

                if (user != null)
                {
                    if (!await userManager.IsEmailConfirmedAsync(user.Id))
                    {
                        var token = await userManager.GenerateEmailConfirmationTokenAsync(user.Id);
                        var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = token }, protocol: Request.Url.Scheme);
                        var mailer = new UserMailer();
                        mailer.ConfirmEmail(user, callbackUrl).Send();

                        AddError("Error", Translations.Validations.ComfirmEmailError);
                        DisplayMessage(Translations.Validations.ComfirmEmailError, MessageType.Warning);
                        return RedirectToAction("Login");
                    }

                    var result = await signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

                    if (result == SignInStatus.Success)
                    {
                        if (!string.IsNullOrEmpty(returnUrl))
                            return RedirectToLocal(returnUrl);
                        else
                            return RedirectToRole();
                    }
                }
            }

            AddError("Error", Translations.Validations.LoginAttemptError);
            return RedirectToAction("Login");
        }
Ejemplo n.º 2
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var appMember = new AppMember
                {
                    UserName = model.Email,
                    Email = model.Email,
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    PictureUrl = "/img/profiles/default.png"
                };

                var result = await userManager.CreateAsync(appMember, model.Password);
                
                if (result.Succeeded)
                {
                    var currentUser = await userManager.FindByNameAsync(model.Email);
                    await userManager.AddToRoleAsync(currentUser.Id, "Autor");
                    //await signInManager.SignInAsync(currentUser, isPersistent: false, rememberBrowser: false);

                    var token = await userManager.GenerateEmailConfirmationTokenAsync(currentUser.Id);
                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = currentUser.Id, code = token }, protocol: Request.Url.Scheme);

                    var mailer = new UserMailer();
                    mailer.ConfirmEmail(currentUser, callbackUrl).Send();
                    ViewBag.Email = currentUser.Email;
                    return View("CheckEmail");
                }

                AddErrors(result);
            }

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