-
-
Save odenijs/5029408 to your computer and use it in GitHub Desktop.
This file contains 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
@model UmbracoLogin.MemberLoginModel | |
@if (User.Identity.IsAuthenticated) | |
{ | |
<p>Logged in: @User.Identity.Name</p> | |
<p>@Html.ActionLink("Log out", "MemberLogout", "MemberLoginSurface")</p> | |
} | |
else | |
{ | |
using (Html.BeginUmbracoForm("MemberLogin", "MemberLoginSurface")) | |
{ | |
@Html.EditorFor(x => Model) | |
<input type="submit" /> | |
} | |
<p>@TempData["Status"]</p> | |
} |
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Web.Mvc; | |
using System.Web.Security; | |
using umbraco.cms.businesslogic.member; | |
using System.ComponentModel.DataAnnotations; | |
namespace UmbracoLogin | |
{ | |
public class MemberLoginModel | |
{ | |
[Required, Display(Name = "User name")] | |
public string Username { get; set; } | |
[Required, Display(Name = "Password"), DataType(DataType.Password)] | |
public string Password { get; set; } | |
[Display(Name = "Remember me")] | |
public bool RememberMe { get; set; } | |
} | |
public class MemberLoginSurfaceController : Umbraco.Web.Mvc.SurfaceController | |
{ | |
[HttpGet] | |
[ActionName("MemberLogin")] | |
public ActionResult MemberLoginGet() | |
{ | |
return PartialView("MemberLogin", new MemberLoginModel()); | |
} | |
[HttpGet] | |
public ActionResult MemberLogout() | |
{ | |
Session.Clear(); | |
FormsAuthentication.SignOut(); | |
return Redirect("/"); | |
} | |
[HttpPost] | |
[ActionName("MemberLogin")] | |
public ActionResult MemberLoginPost(MemberLoginModel model, string returnUrl) | |
{ | |
if (ModelState.IsValid) | |
{ | |
if (Membership.ValidateUser(model.Username, model.Password)) | |
{ | |
FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe); | |
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") | |
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) | |
{ | |
return Redirect(returnUrl); | |
} | |
else | |
{ | |
return RedirectToCurrentUmbracoPage(); | |
} | |
} | |
else | |
{ | |
TempData["Status"] = "Invalid username or password"; | |
return RedirectToCurrentUmbracoPage(); | |
} | |
} | |
TempData["Status"] = "Invalid request"; | |
return RedirectToCurrentUmbracoPage(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice piece of code. Good use of surface controllers for authentication. Secure too.