public ActionResult Login(LoginViewModel model, string returnUrl = "")
        {
            if (ModelState.IsValid)
            {
                //var user = (Object)null;
                var user = Context.Users.Where(u => u.UserName.ToUpper() == model.UserName.ToUpper() && u.Pass == model.Pass).FirstOrDefault();
                //var user = await UserManager.FindAsync(model.UserName, model.Pass);
                if (user != null)
                {
                    //await SignInAsync(user, model.RememberMe);

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

                    CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
                    serializeModel.UserID = user.UserID;
                    serializeModel.Roles = roles;

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

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

                    if (roles.Contains("Admin"))
                    {
                        return RedirectToAction("Index", "Admin");
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }

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

            return View(model);
        }
        public ActionResult Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    var user = Context.Users.Where(u => u.UserName.ToUpper() == model.UserName.ToUpper()).FirstOrDefault();
                    if (user != null)
                    {
                        ModelState.AddModelError("", "Username is already used");
                        return View(model);
                    }

                    user = new User()
                    {
                        UserName = model.UserName,
                        Pass = model.Pass,
                        Roles = new List<Role>()
                    };
                    var role = Context.Roles.Where(r => r.RoleName == "User").FirstOrDefault();
                    user.Roles.Add(role);
                    Context.Users.Add(user);
                    Context.SaveChanges();

                    var roles = user.Roles.Select(m => m.RoleName).ToArray();
                    CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
                    serializeModel.UserID = user.UserID;
                    serializeModel.Roles = roles;

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

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

                    if (roles.Contains("Admin"))
                    {
                        return RedirectToAction("Index", "Admin");
                    }
                    else
                    {
                        return RedirectToAction("Index", "Book");
                    }
                }
                catch
                {
                    ModelState.AddModelError("", "Cannot create user account");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }