public ActionResult HandleLogin(LoginViewModel model) { if (!ModelState.IsValid) { //return RedirectToCurrentUmbracoPage(); return PartialView("Login", model); } //Member already logged in - redirect to home if (Member.IsLoggedOn()) { return Redirect("/"); } //Lets TRY to log the user in try { //Try and login the user... var attemptLogin = Member.GetMemberFromLoginNameAndPassword(model.EmailAddress, model.Password); if(attemptLogin != null) { //Valid credentials //Get the member from their email address var checkMember = Member.GetMemberFromEmail(model.EmailAddress); //Check the member exists if (checkMember != null) { //Let's check they have verified their email address if (Convert.ToBoolean(checkMember.getProperty("hasVerifiedEmail").Value)) { //Update number of logins counter int noLogins = 0; if (int.TryParse(checkMember.getProperty("numberOfLogins").Value.ToString(), out noLogins)) { //Managed to parse it to a number //Don't need to do anything as we have default value of 0 } //Update the counter checkMember.getProperty("numberOfLogins").Value = noLogins + 1; //Update label with last login date to now checkMember.getProperty("lastLoggedIn").Value = DateTime.Now.ToString("dd/MM/yyyy @ HH:mm:ss"); //Update label with last logged in IP address & Host Name string hostName = Dns.GetHostName(); string clientIPAddress = Dns.GetHostAddresses(hostName).GetValue(0).ToString(); try { checkMember.getProperty("hostNameOfLastLogin").Value = hostName; checkMember.getProperty("IPofLastLogin").Value = clientIPAddress; } catch { } //Save the details checkMember.Save(); //If they have verified then lets log them in //Set Auth cookie FormsAuthentication.SetAuthCookie(model.EmailAddress, true); //Once logged in - redirect them back to the return URL return new RedirectResult(model.ReturnUrl); } else { //User has not verified their email yet ModelState.AddModelError("LoginForm.", "Email account has not been verified"); return CurrentUmbracoPage(); } } } else { ModelState.AddModelError("LoginForm.", "Invalid details"); return CurrentUmbracoPage(); } } catch (Exception ex) { ModelState.AddModelError("LoginForm.", "Error: " + ex.ToString()); return CurrentUmbracoPage(); } return CurrentUmbracoPage(); }
/// <summary> /// Renders the Login view /// @Html.Action("RenderLogin","AuthSurface"); /// </summary> /// <returns></returns> public ActionResult RenderLogin() { LoginViewModel loginModel = new LoginViewModel(); if (string.IsNullOrEmpty(HttpContext.Request["ReturnUrl"])) { //If returnURL is empty then set it to / loginModel.ReturnUrl = "/"; } else { //Lets use the return URL in the querystring or form post loginModel.ReturnUrl = HttpContext.Request["ReturnUrl"]; } return PartialView("Login", loginModel); }