Skip to content

Instantly share code, notes, and snippets.

@odenijs
Forked from anonymous/MemberLogin.cshtml
Created February 25, 2013 12:10
Show Gist options
  • Save odenijs/5029408 to your computer and use it in GitHub Desktop.
Save odenijs/5029408 to your computer and use it in GitHub Desktop.
@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>
}
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();
}
}
}
@csharpforevermore
Copy link

Nice piece of code. Good use of surface controllers for authentication. Secure too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment