public ActionResult ManageLogin(LoginEditModel model) { if (!ModelState.IsValid) { return(View(model)); } User user = model.AsUser(); UserLoginStatus userLoginStatus = membershipService.ValidateUser(user.UserName, user.Password); if (userLoginStatus == UserLoginStatus.InvalidCredentials) { IUser userByEmail = userService.FindUserByEmail(user.UserName); if (userByEmail != null) { user = userByEmail as User; userLoginStatus = membershipService.ValidateUser(userByEmail.UserName, model.Password); } } else { user = userService.GetFullUser(model.UserName); } if (userLoginStatus == UserLoginStatus.InvalidCredentials) { ViewData["StatusMessageData"] = new StatusMessageData(StatusMessageType.Error, "帐号或密码错误,请重新输入!"); } if (userLoginStatus == UserLoginStatus.Success) { if (user.IsAllowEntryControlPannel()) { HttpCookie adminCookie = new HttpCookie("SpacebuilderAdminCookie" + user.UserId); adminCookie.Value = Utility.EncryptTokenForAdminCookie(true.ToString()); if (!string.IsNullOrEmpty(FormsAuthentication.CookieDomain)) { adminCookie.Domain = FormsAuthentication.CookieDomain; } adminCookie.HttpOnly = true; Response.Cookies.Add(adminCookie); FormsAuthentication.SetAuthCookie(model.UserName, false); } else { return(Redirect(SiteUrls.Instance().ManageLogin())); } string redirectUrl = null; if (!string.IsNullOrEmpty(model.ReturnUrl)) { redirectUrl = model.ReturnUrl; } else { redirectUrl = SiteUrls.Instance().ManageHome(); } return(Redirect(redirectUrl)); } return(View(model)); }
public ActionResult LoginInModel(LoginEditModel model) { if (!ModelState.IsValid) { Tunynet.Utilities.WebUtility.SetStatusCodeForError(Response); return View("_LoginInModal", model); } //尝试登录 User user = model.AsUser(); //使用用户名作为用户名和邮件分别尝试登录 UserLoginStatus userLoginStatus = membershipService.ValidateUser(user.UserName, user.Password); if (userLoginStatus == UserLoginStatus.InvalidCredentials) { IUser userByEmail = userService.FindUserByEmail(user.UserName); if (userByEmail != null) { user = userByEmail as User; userLoginStatus = membershipService.ValidateUser(userByEmail.UserName, model.Password); } if (userLoginStatus != UserLoginStatus.InvalidCredentials && !userByEmail.IsEmailVerified) { return Json(new StatusMessageData(StatusMessageType.Error, "您的邮箱没有通过验证,请使用帐号登录")); } } else { user = userService.GetUser(model.UserName) as User; } if (userLoginStatus == UserLoginStatus.Success || (userLoginStatus == UserLoginStatus.NotActivated && userSettings.EnableNotActivatedUsersToLogin)) { Response.Cookies["returnUrl"].Expires = DateTime.Now; //让用户登录 user.UserId = UserIdToUserNameDictionary.GetUserId(user.UserName); authenticationService.SignIn(user, model.RememberPassword); return Json(new StatusMessageData(StatusMessageType.Success, "登录成功")); } string message; switch (userLoginStatus) { case UserLoginStatus.Banned: message = "用户被封禁"; break; case UserLoginStatus.InvalidCredentials: message = "用户名、密码不匹配"; break; case UserLoginStatus.NotActivated: message = "帐号未激活"; break; default: message = "未知错误"; break; } ViewData["StatusMessageData"] = new StatusMessageData(StatusMessageType.Error, message); Tunynet.Utilities.WebUtility.SetStatusCodeForError(Response); return View("_LoginInModal", model); }
public ActionResult ManageLogin(LoginEditModel model) { if (!ModelState.IsValid) return View(model); User user = model.AsUser(); UserLoginStatus userLoginStatus = membershipService.ValidateUser(user.UserName, user.Password); if (userLoginStatus == UserLoginStatus.InvalidCredentials) { IUser userByEmail = userService.FindUserByEmail(user.UserName); if (userByEmail != null) { user = userByEmail as User; userLoginStatus = membershipService.ValidateUser(userByEmail.UserName, model.Password); } } else { user = userService.GetFullUser(model.UserName); } if (userLoginStatus == UserLoginStatus.InvalidCredentials) { ViewData["StatusMessageData"] = new StatusMessageData(StatusMessageType.Error, "帐号或密码错误,请重新输入!"); } if (userLoginStatus == UserLoginStatus.Success) { if (user.IsAllowEntryControlPannel()) { HttpCookie adminCookie = new HttpCookie("SpacebuilderAdminCookie" + user.UserId); adminCookie.Value = Utility.EncryptTokenForAdminCookie(true.ToString()); if (!string.IsNullOrEmpty(FormsAuthentication.CookieDomain)) adminCookie.Domain = FormsAuthentication.CookieDomain; adminCookie.HttpOnly = true; Response.Cookies.Add(adminCookie); FormsAuthentication.SetAuthCookie(model.UserName, false); } else return Redirect(SiteUrls.Instance().ManageLogin()); string redirectUrl = null; if (!string.IsNullOrEmpty(model.ReturnUrl)) redirectUrl = model.ReturnUrl; else redirectUrl = SiteUrls.Instance().ManageHome(); return Redirect(redirectUrl); } return View(model); }
public ActionResult Login(LoginEditModel model) { pageResourceManager.InsertTitlePart("登录"); ViewData["CanRegister"] = userSettings.RegistrationMode == RegistrationMode.All; if (!ModelState.IsValid) { model.Password = string.Empty; return View(model); } //尝试登录 User user = model.AsUser(); //使用用户名作为用户名和邮件分别尝试登录 UserLoginStatus userLoginStatus = membershipService.ValidateUser(user.UserName, user.Password); if (userLoginStatus == UserLoginStatus.InvalidCredentials) { IUser userByEmail = userService.FindUserByEmail(user.UserName); if (userByEmail != null) { user = userByEmail as User; userLoginStatus = membershipService.ValidateUser(userByEmail.UserName, model.Password); } if (userLoginStatus != UserLoginStatus.InvalidCredentials && !userByEmail.IsEmailVerified) { ViewData["StatusMessage"] = new StatusMessageData(StatusMessageType.Error, "您的邮箱没有激活,请您使用帐号登录"); model.Password = string.Empty; return View(model); } } else { user = userService.GetUser(model.UserName) as User; } if (userLoginStatus == UserLoginStatus.Success || (userLoginStatus == UserLoginStatus.NotActivated && userSettings.EnableNotActivatedUsersToLogin)) { //让用户登录 user.UserId = UserIdToUserNameDictionary.GetUserId(user.UserName); authenticationService.SignIn(user, model.RememberPassword); } if (userLoginStatus == UserLoginStatus.Success) { if (Request.Cookies["invite"] != null) { InvitationCode invitationCode = inviteFriendService.GetInvitationCodeEntity(Request.Cookies["invite"].Value); if (invitationCode != null) { Response.Cookies.Remove("invite"); string token = Utility.EncryptTokenForInviteFriend(0.01, user.UserId); return Redirect(SiteUrls.Instance().ConfirmFollow(token, invitationCode.Code)); } } if (Request.Cookies.Get("returnUrl") != null) { Response.Cookies["returnUrl"].Expires = DateTime.Now; } if (!string.IsNullOrEmpty(model.ReturnUrl)) return Redirect(Tunynet.Utilities.WebUtility.UrlDecode(model.ReturnUrl)); if (Request.Cookies != null) { string returnUrl = Request.Cookies.Get("returnUrl") != null ? Request.Cookies.Get("returnUrl").Value : string.Empty; if (!string.IsNullOrEmpty(returnUrl)) return Redirect(Tunynet.Utilities.WebUtility.UrlDecode(returnUrl)); } if (model.loginInModal && Request.UrlReferrer != null) return Redirect(Request.UrlReferrer.AbsoluteUri); //判断站点设置选择登录之后的页面 if (userSettings.MyHomePageAsSiteEntry) return Redirect(SiteUrls.Instance().MyHome(user.UserId)); return Redirect(SiteUrls.Instance().SiteHome()); } else if (userLoginStatus == UserLoginStatus.InvalidCredentials) { ViewData["StatusMessage"] = new StatusMessageData(StatusMessageType.Error, "帐号密码不匹配,请检查您的帐号密码"); model.Password = string.Empty; return View(model); } else if (userLoginStatus == UserLoginStatus.Banned) { return Redirect(SiteUrls.Instance().SystemMessage(TempData, new SystemMessageViewModel { Title = "帐号被封禁!", Body = "不好意思,由于您的非法操作,帐号已经被封禁,封禁截止到" + user.BanDeadline.ToFriendlyDate(), StatusMessageType = StatusMessageType.Error }, model.ReturnUrl)); } else if (userLoginStatus == UserLoginStatus.NotActivated) { string token = Utility.EncryptTokenForValidateEmail(0.004, user.UserId); return Redirect(SiteUrls.Instance().SystemMessage(TempData, new SystemMessageViewModel { Title = "帐号未激活!", Body = "您的帐号还未激活,请尽快{0}您的帐号,以方便您更多操作!", BodyLink = new Dictionary<string, string> { { "激活", SiteUrls.Instance()._ActivateByEmail(user.AccountEmail, token) } }, StatusMessageType = StatusMessageType.Hint }, model.ReturnUrl)); } ViewData["StatusMessage"] = new StatusMessageData(StatusMessageType.Error, "系统发生未知错误"); model.Password = string.Empty; return View(model); }