public ActionResult Login(LoginViewModel avm, string returnUrl = "") { var am = new MyUserManager(); if (!ModelState.IsValid || !Membership.ValidateUser(avm.Username, avm.Password)) { ViewBag.Error = "Account credentials are invalid"; return(View("Login")); } var user = am.Find(avm.Username, avm.Password); var js = new JavaScriptSerializer(); var data = js.Serialize(user); var ticket = new FormsAuthenticationTicket(1, avm.Username, DateTime.Now, DateTime.Now.AddMinutes(30), avm.RememberMe, data); var encToken = FormsAuthentication.Encrypt(ticket); var cockie = new HttpCookie(FormsAuthentication.FormsCookieName, encToken); Response.Cookies.Add(cockie); //FormsAuthentication.SetAuthCookie(avm.Username, avm.RememberMe); if (Url.IsLocalUrl(returnUrl)) { return(Redirect(returnUrl)); } else { return(RedirectToAction("Index", "Profile")); } }
public ActionResult Login(string username, string password) { Account account = _userManager.Find(username, password); if (account == null) { return(HttpNotFound()); } // success var ident = _userManager.CreateIdentity(account, DefaultAuthenticationTypes.ApplicationCookie); //use the instance that has been created. var authManager = HttpContext.GetOwinContext().Authentication; authManager.SignIn( new AuthenticationProperties { IsPersistent = false }, ident); return(Redirect("/Home")); }
public override bool ValidateUser(string username, string password) { var am = new MyUserManager(); return(am.Find(username, password) != null); }
public ResponseResult ChangePass(HSKR.Identity.ChangePasswordBindingModel model) { try { if (!model.NewPassword.Trim().Equals(model.ConfirmPassword.Trim())) { var data = new Response <UserViewModel> { Message = HttpMessage.CHANGE_PASS_OLD_PASS, Data = null, Status = false }; ActionContext.Response.StatusCode = HttpStatusCode.Redirect; return(new ResponseResult(data, ActionContext)); } var user = userManager.Find(model.UserName, model.OldPassword); if (user == null) { var data = new Response <UserViewModel> { Message = HttpMessage.CHANGE_PASS_WRONG_ACC, Data = null, Status = false }; ActionContext.Response.StatusCode = HttpStatusCode.Redirect; return(new ResponseResult(data, ActionContext)); } var result = JObject.Parse(user.ToJson()); var userId = result["id"]; var changePassResult = userManager.ChangePasswordAsync(long.Parse(userId.ToString()), model.OldPassword, model.NewPassword.Trim()); if (!changePassResult.Result.Succeeded) { var repon = new Response <UserViewModel> { Message = changePassResult.Result.Errors.ToJson(), Status = false, }; ActionContext.Response.StatusCode = HttpStatusCode.InternalServerError; return(new ResponseResult(repon, ActionContext)); } var userResponse = new Response <UserViewModel> { Message = "Change password success", Status = true, }; return(new ResponseResult(userResponse, ActionContext)); } catch (Exception exception) { logger.Error(exception); ActionContext.Response.StatusCode = HttpStatusCode.InternalServerError; var data = new Response <UserViewModel> { Message = HttpMessage.ERROR_CHANGE_PASS, Status = false }; return(new ResponseResult(data, ActionContext)); } }