Skip to content

Instantly share code, notes, and snippets.

@Vikaskumargd
Created January 16, 2017 19:14
Show Gist options
  • Save Vikaskumargd/a45aca46ec736774029f78609839993d to your computer and use it in GitHub Desktop.
Save Vikaskumargd/a45aca46ec736774029f78609839993d to your computer and use it in GitHub Desktop.
I felt the same way when taking a look at Identity. It added lots of abstractions and does not suit with my case that I have legacy system which implemented own authentication work-flow. Tons of example about OWIN authentication which uses Identity and EF by default. But technically, you are able to strip out Identity to use only OWIN cookie aut…
[HttpPost]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
var user = _userService.GetByEmail(model.Email);
//check username and password from database, naive checking: password should be in SHA
if (user != null && (user.Password == model.Password))
{
var claims = new[] {
new Claim(ClaimTypes.Name, user.Name),
new Claim(ClaimTypes.Email, user.Email),
// can add more claims
};
var identity = new ClaimsIdentity(claims, "ApplicationCookie");
// Add roles into claims
var roles = _roleService.GetByUserId(user.Id).ToList();
if (roles.Any())
{
var roleClaims = roles.Select(r => new Claim(ClaimTypes.Role, r.Name));
identity.AddClaims(roleClaims);
}
var context = Request.GetOwinContext();
var authManager = context.Authentication;
authManager.SignIn(new AuthenticationProperties { IsPersistent = model.RememberMe }, identity);
return RedirectToAction("Index", "Home");
}
// login failed.
}
public ActionResult LogOut()
{
var ctx = Request.GetOwinContext();
var authManager = ctx.Authentication;
authManager.SignOut("ApplicationCookie");
return RedirectToAction("Login");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment