コード例 #1
0
 public ActionResult Login(string returnUrl, string actionPerformed)
 {
     ViewBag.ReturnUrl = returnUrl;
     var model = new LoginViewModel();
     model.ActionPerformed = !string.IsNullOrWhiteSpace(actionPerformed);
     return View(model);
 }
コード例 #2
0
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            model.CleanModelState(ModelState);

            if (ModelState.IsValid)
            {
                var user = await UserManager.FindAsync(model.UserName, model.Password);
                if (user != null)
                {
                    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                    var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
                    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = model.RememberMe }, identity);

                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    model.ValidationSummary.AddMessage("Nome de usuário e/ou senha inválido(s).");
                }
            }
            else
            {

                var errors = ModelState.Values.SelectMany(v => v.Errors.Select(i => i.ErrorMessage)).ToList();
                model.ValidationSummary.AddMessages(errors);
            }

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