public ActionResult Login(LoginViewModel model, string returnUrl) { string name = Request.Params["name"]; string password = Request.Params["password"]; MyPrincipal user = new MyPrincipal(name, password); if (user.Identity.IsAuthenticated) { userSignIn(user); if (CommonLib.Common.Validate.IsNullString(returnUrl) == "") { return RedirectToRoute("manage-index"); } else { return RedirectToLocal(returnUrl); } } else { ModelState.AddModelError("", "Invalid username or password."); } //if (ModelState.IsValid) //{ // AccountLib.UserHandle userHandle = new AccountLib.UserHandle(); // System.Data.DataSet ds = userHandle.Login(model.UserName, model.Password); // if (ds != null) // { // IdentityUserRole userRole = new IdentityUserRole(); // IdentityRole role = new IdentityRole(); // ApplicationUser user = new ApplicationUser(); // user.Id = ds.Tables[0].Rows[0]["UserID"].ToString(); // user.UserName = model.UserName; // user.PasswordHash = model.Password; // role.Id = ds.Tables[0].Rows[0]["UserTypeID"].ToString(); // role.Name = ds.Tables[0].Rows[0]["UserTypeText"].ToString(); // userRole.RoleId = role.Id; // userRole.Role = role; // userRole.UserId = user.Id; // userRole.User = user; // user.Roles.Add(userRole); // //SignInAsync(user, false);//model.RememberMe // HttpContext.User = // return RedirectToLocal(returnUrl); // } // else // { // ModelState.AddModelError("", "Invalid username or password."); // } //} ViewData["title"] = "系统登陆"; // 如果我们进行到这一步时某个地方出错,则重新显示表单 return View(model); }
private void userSignIn(MyPrincipal user) { AccountLib.UserHandle userHandle = new AccountLib.UserHandle(); CacheLib.Cache cache = new CacheLib.Cache(); string key = cache.Add<MyPrincipal>("user", user); userHandle.saveTicket(key); }