Skip to content

Instantly share code, notes, and snippets.

@ChaseFlorell
Created March 22, 2013 01:36
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 ChaseFlorell/5218298 to your computer and use it in GitHub Desktop.
Save ChaseFlorell/5218298 to your computer and use it in GitHub Desktop.
Specifically for StackOverflow Question - http://stackoverflow.com/q/15560855/124069
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
// check if a valid user was selected
var user = GetuserByname(model.UserName);
if (user.ToList().Count != 1)
{
ModelState.AddModelError("Username", "Username is invalid");
}
var dbPassword = user.First().UserPassword.ToString().Trim();
var enterPassword = CreatePasswordHash(model.Password, user.First().Salt.ToString().Trim());
if (dbPassword != enterPassword)
{
ModelState.AddModelError("dbPassword", "Password is invalid");
}
if (!ModelState.IsValid) {
return View(model);
}
FormsAuthentication.SetAuthCookie(user.First().tblUserRole.RoleName, model.RememberMe);
Session["logged"] = user.First().Username;
string roleName = user.First().tblUserRole.RoleName;
Roles.AddUserToRole(model.UserName, roleName);
return RedirectToAction("Index", "Home");
}
@ChaseFlorell
Copy link
Author

This is just a modification of the original code posted. It's modified to clean up the Cyclomatic Complexity... just a little bit. Deeply nested if statements kinda get under my skin.

I hope this is helpful.

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