Skip to content

Instantly share code, notes, and snippets.

@xrisdoc
Last active December 19, 2015 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xrisdoc/55786c825542777d96ac to your computer and use it in GitHub Desktop.
Save xrisdoc/55786c825542777d96ac to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CPS.UI.WebApp.Models;
using System.Web.Security;
namespace CPS.UI.WebApp.Controllers
{
public class LoginController : Controller
{
//
// GET: /Login/
[HttpGet]
public ActionResult Index(string returnUrl)
{
// The login form/page will only be avaialble to user who are not currently logged in.
// If the user is currently ogged in, redirect them away from the login page.
if (User.Identity.IsAuthenticated) return RedirectToAction("Index", "Home");
LoginFormModel model = new LoginFormModel();
if (returnUrl != null && returnUrl.Trim().Length > 0) model.ReturnUrl = returnUrl.Trim();
return View(model);
}
//
// POST: /Login/
[HttpPost]
public ActionResult Index(LoginFormModel model)
{
if (ModelState.IsValid)
{
bool isLoggedIn = model.TryToLogin();
if (isLoggedIn == false)
{
ModelState.AddModelError("LoginError", "Must enter a valid username/password");
}
else
{
string returnUrl = model.ReturnUrl;
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
return Redirect(returnUrl);
else
return RedirectToAction("Index", "Home");
}
}
return View(model);
}
//
// GET: /Logout/
[HttpGet]
public ActionResult Logout()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Security;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.member;
namespace CPS.UI.WebApp.Models
{
public class LoginFormModel : BaseModels.GenericPageModel
{
/// <summary>
/// The default title to use as the page title
/// </summary>
private const string defaultPageTitle = "Login";
/// <summary>
///
/// </summary>
public LoginFormModel()
{
this.PageTitle = defaultPageTitle;
this.MetaTags.Title = this.PageTitle;
}
[Required(ErrorMessage = "Username is required")]
public string Username { get; set; }
[Required(ErrorMessage = "Password is required")]
public string Password { get; set; }
/// <summary>
/// The URL that the user will be redirected to after they succesfully login
/// </summary>
public string ReturnUrl { get; set; }
public bool TryToLogin()
{
string username = this.Username.ToLower().Trim();
string password = this.Password;
if (Membership.ValidateUser(username, password)) // Error in umbraco.cms.businesslogic.member.Member.Save() is thrown here
{
MembershipUser m = Membership.GetUser(username);
if (m != null)
{
FormsAuthentication.SetAuthCookie(m.UserName, false);
return true;
}
}
// Return false by default
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment