Last active
November 23, 2018 16:54
-
-
Save warrenbuckley/5685180 to your computer and use it in GitHub Desktop.
Login ActionResult in AuthSurfaceController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Handles the login form when user posts the form/attempts to login | |
/// </summary> | |
/// <param name="model"></param> | |
/// <returns></returns> | |
[HttpPost] | |
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... | |
if (Membership.ValidateUser(model.EmailAddress, model.Password)) | |
{ | |
//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(); | |
checkMember.getProperty("hostNameOfLastLogin").Value = hostName; | |
checkMember.getProperty("IPofLastLogin").Value = clientIPAddress; | |
//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(); | |
//return PartialView("Login", model); | |
} | |
} | |
} | |
else | |
{ | |
ModelState.AddModelError("LoginForm.", "Invalid details"); | |
return CurrentUmbracoPage(); | |
//return PartialView("Login", model); | |
} | |
} | |
catch (Exception ex) | |
{ | |
ModelState.AddModelError("LoginForm.", "Error: " + ex.ToString()); | |
return CurrentUmbracoPage(); | |
//return PartialView("Login", model); | |
} | |
//Update success flag | |
TempData["IsSuccessful"] = true; | |
return RedirectToCurrentUmbracoPage(); | |
} | |
///Login Partial from Surface Controller | |
@using Site.Controllers.SurfaceControllers | |
@using Site.Models | |
@model LoginViewModel | |
@{ | |
Html.EnableClientValidation(true); | |
Html.EnableUnobtrusiveJavaScript(true); | |
} | |
@if (!ViewData.ModelState.IsValid) | |
{ | |
@foreach (ModelState modelState in ViewData.ModelState.Values) | |
{ | |
var errors = modelState.Errors; | |
if (errors.Any()) | |
{ | |
<ul> | |
@foreach(ModelError error in errors) | |
{ | |
<li><em>@error.ErrorMessage</em></li> | |
} | |
</ul> | |
} | |
} | |
} | |
@using(Html.BeginUmbracoForm<AuthSurfaceController>("HandleLogin")) | |
{ | |
@Html.ValidationSummary(true) | |
<fieldset> | |
<legend>Login</legend> | |
<div class="editor-label"> | |
@Html.LabelFor(model => model.EmailAddress) | |
</div> | |
<div class="editor-field"> | |
@Html.EditorFor(model => model.EmailAddress) | |
@Html.ValidationMessageFor(model => model.EmailAddress) | |
</div> | |
<div class="editor-label"> | |
@Html.LabelFor(model => model.Password) | |
</div> | |
<div class="editor-field"> | |
@Html.EditorFor(model => model.Password) | |
@Html.ValidationMessageFor(model => model.Password) | |
</div> | |
@Html.HiddenFor(Model => Model.ReturnUrl) | |
<p> | |
<input type="submit" value="Login" /> | |
</p> | |
</fieldset> | |
} | |
///Login Template | |
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage | |
@{ | |
Layout = "Master.cshtml"; | |
} | |
<div class="row"> | |
<div class="large-8 large-centered columns"> | |
<h1>Login</h1> | |
@Html.Action("RenderLogin","AuthSurface") | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment