コード例 #1
0
        public JsonResult Login(LoginViewModel model, string returnUrl = "")
        {
            if (ModelState.IsValid)
            {
                var user = _authService.CheckUser(model.Email, model.Password);

                if (user != null)
                {
                    var roles = _authService.PermissionActiveNameList(user.Id).ToList();

                    CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
                    serializeModel.UserId    = user.Id;
                    serializeModel.Email     = user.Email;
                    serializeModel.FirstName = user.FirstName;
                    serializeModel.LastName  = user.LastName;
                    serializeModel.roles     = roles;

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

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

                    //if (roles.Contains("Admin"))
                    //{
                    //    return RedirectToAction("Index", "Admin");
                    //}
                    //else if (roles.Contains("User"))
                    //{
                    //    return RedirectToAction("Index", "User");
                    //}
                    //else
                    //{
                    return(Json(new
                    {
                        redirectUrl = Url.Action("Index", "Home"),
                        isRedirect = true
                    }));
                    //}
                }

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

            return(Json(model));
        }