public LoginResponse Login(LoginViewModel model) { var userId = _service.ValidateUser(model.Login, model.Password); if (userId.HasValue) { var userInfo = _service.GetUserInfo(userId.Value); var ticket = new FormsAuthenticationTicket(1, userInfo.Nickname, DateTime.Now, DateTime.Now.AddDays(1), model.Persistent, userId.Value.ToString()); var ticketStr = FormsAuthentication.Encrypt(ticket); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketStr); return new LoginResponse { Name = userInfo.Nickname, Cookie = cookie, Role = userInfo.Role.Name }; } return null; }
public ActionResult Login(LoginViewModel model, String returnUrl) { if (!User.Identity.IsAuthenticated && ModelState.IsValid) { var responseObj = _logics.Login(model); if (responseObj != null) { Response.Cookies.Add(responseObj.Cookie); var identity = new GenericIdentity(responseObj.Name); HttpContext.User = new GenericPrincipal(identity, new[] { responseObj.Role }); var a = User.IsInRole("Admin"); } else { ModelState.AddModelError("login/password", "Login or password is incorrent"); return View(); } } return RedirectToAction("Index", "Home"); }