コード例 #1
0
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var user = _AccountService.CheckUsuario(model.UserName);

                if (user != null)
                {
                    Session["paginas"]  = _CatalogoService.FindPagina(0);
                    Session["permisos"] = _AccountService.GetAccionUsuario(user.UsuarioID);

                    //var roles = user.Roles.Select(m => m.RoleName).ToArray();

                    var hashCode = user.VCode;
                    //Password Hasing Process Call Helper Class Method
                    var encodingPasswordString = Helper.EncodePassword(model.Password, hashCode);
                    //Check Login Detail User Name Or Password
                    if (user.Contraseña.Equals(encodingPasswordString))
                    {
                        CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
                        serializeModel.UserId   = user.UsuarioID;
                        serializeModel.UserName = user.NombreUsuario;

                        string userData = JsonConvert.SerializeObject(serializeModel);
                        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                            1,
                            user.NombreUsuario,
                            DateTime.Now,
                            DateTime.Now.AddMinutes(2880),
                            false,
                            userData);

                        string     encTicket = FormsAuthentication.Encrypt(authTicket);
                        HttpCookie faCookie  = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                        Response.Cookies.Add(faCookie);

                        return(RedirectToAction("Index", "Home"));
                    }
                }

                ModelState.AddModelError("", "Incorrect username and/or password");
            }

            return(View(model));
        }