public ActionResult Login(LoginModel model, string returnUrl) { string role_redirect_url = ""; // Redirect to the View based on Role // /Role/Dashbaord #region [ Initial Admin Setup ] if (model.UserName == "SetupAdmin") { if (!WebSecurity.UserExists("admin")) { return RedirectToLocal("/Admin/SetupAdmin"); } else { ModelState.AddModelError("", "Admin user has already been setup."); return View(model); } } #endregion if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe)) { // Get the Roles return RedirectToLocal(role_redirect_url); } // If we got this far, something failed, redisplay form ModelState.AddModelError("", "The user name or password provided is incorrect."); return View(model); }
public ActionResult Login(LoginModel model, string returnUrl) { // HTTP Context Infomration var headers = Request.ServerVariables; // Eval Username input format then try to authenticate using differnet means // Attempt Local bool IsLocal = (model.UserName.Contains((@"\"))); // Attempt AD bool IsActiveDirectory = (model.UserName.Contains("@")); // Is ASP Membership USer bool IsMembership = ((IsLocal && IsActiveDirectory) == false); if (IsMembership) { if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe)) { return RedirectToLocal(returnUrl); } } if (IsActiveDirectory) { using (PrincipalContext context = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["security.domain_name"])) { if (context.ValidateCredentials(model.UserName, model.Password)) { if (!WebSecurity.UserExists(model.UserName)) { WebSecurity.CreateUserAndAccount(model.UserName, model.Password); } return RedirectToLocal(returnUrl); } } } if (IsLocal) { using (PrincipalContext context = new PrincipalContext(ContextType.Machine,Environment.MachineName)) { if (context.ValidateCredentials(model.UserName, model.Password)) { if (!WebSecurity.UserExists(model.UserName)) { WebSecurity.CreateUserAndAccount(model.UserName, model.Password); } return RedirectToLocal(returnUrl); } } } // If we got this far, something failed, redisplay form ModelState.AddModelError(string.Empty, "The user name or password provided is incorrect."); return View(model); }