public ActionResult Login(LoginModel model, string ReturnUrl) { if (Request.UrlReferrer == null) return RedirectToAction("Error", "Shared", new { msg = "登录姿势不对" }); ViewBag.ReturnUrl = ReturnUrl; if (!ModelState.IsValid) return View(model); string token; try { token = USER.Login(model.UserName, model.Password, Request.Headers["X-Forwarded-For"] ?? Request.UserHostAddress); } catch (UserNotFoundException) { ModelState.AddModelError("UserName", "用户名不存在"); return View(model); } catch (PasswordMismatchException) { ModelState.AddModelError("Password", "密码错误"); return View(model); } FormsAuthentication.SetAuthCookie(token, model.KeepOnline); string referrer = Request.UrlReferrer.ToString(); if (ReturnUrl != null) { return Redirect(ReturnUrl); } else if (!referrer.Contains("Login") && !referrer.Contains("Error")) { return Redirect(referrer); } else { return Redirect("~"); } }
public ActionResult Login(string ReturnUrl) { ViewBag.ReturnUrl = ReturnUrl; LoginModel model = new LoginModel { KeepOnline = true }; return View(model); }