Skip to content

Instantly share code, notes, and snippets.

@AlexArchive
Last active August 29, 2015 14:04
Show Gist options
  • Save AlexArchive/098c41e8e28d64642474 to your computer and use it in GitHub Desktop.
Save AlexArchive/098c41e8e28d64642474 to your computer and use it in GitHub Desktop.
Minimal Identity Authenticator
public class AccountController : Controller
{
private readonly Authenticator authenticator = new Authenticator();
public ActionResult Login()
{
return View();
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
var authenticated = authenticator.Login(model.Username, model.Password);
if (authenticated)
{
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError("", "Invalid username or password.");
}
return View(model);
}
public ActionResult Register()
{
return View();
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var result = authenticator.Register(model.Username, model.Password);
if (result.Succeeded)
{
return RedirectToAction("Index", "Home");
}
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error);
}
}
return View(model);
}
public ActionResult SignOut()
{
authenticator.SignOut();
return RedirectToAction("Index", "Home");
}
}
public class Authenticator
{
private UserManager<IdentityUser> UserManager
{
get
{
var store = new UserStore<IdentityUser>();
return new UserManager<IdentityUser>(store);
}
}
private IAuthenticationManager AuthenticationManager
{
get { return HttpContext.Current.GetOwinContext().Authentication; }
}
public IdentityResult Register(string username, string password)
{
var user = new IdentityUser { UserName = username };
var result = UserManager.Create(user, password);
if (result.Succeeded)
{
Authenticate(user, false);
}
return result;
}
public bool Login(string username, string password, bool persistant = false)
{
var user = UserManager.Find(username, password);
if (user != null)
{
Authenticate(user, persistant);
return true;
}
return false;
}
public void Authenticate(IdentityUser user, bool persistent)
{
var identity = UserManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
var authenticationProperties = new AuthenticationProperties { IsPersistent = persistent };
AuthenticationManager.SignIn(authenticationProperties, identity);
}
public void SignOut()
{
AuthenticationManager.SignOut();
}
}
public class LoginViewModel
{
[Required]
public string Username { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="bootstrap" version="3.0.0" targetFramework="net45" />
<package id="EntityFramework" version="6.1.1" targetFramework="net45" />
<package id="jQuery" version="1.10.2" targetFramework="net45" />
<package id="Microsoft.AspNet.Identity.Core" version="2.0.1" targetFramework="net45" />
<package id="Microsoft.AspNet.Identity.EntityFramework" version="2.0.1" targetFramework="net45" />
<package id="Microsoft.AspNet.Identity.Owin" version="2.0.1" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc" version="5.1.2" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.1.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="3.1.2" targetFramework="net45" />
<package id="Microsoft.Owin" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.Owin.Host.SystemWeb" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.Owin.Security" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.Owin.Security.Cookies" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.Owin.Security.OAuth" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="Modernizr" version="2.6.2" targetFramework="net45" />
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net45" />
<package id="Owin" version="1.0" targetFramework="net45" />
</packages>
public class RegisterViewModel
{
[Required]
[Display(Name = "Username")]
public string Username { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Login")
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment