Exemplo n.º 1
0
        public ActionResult Login(User model, string returnUrl = "")
        {
            Context.User _user = RepoUser.FindByUsername(model.Username);

            if (_user != null)
            {
                if (Decrypt(_user.Password) != model.Password)
                {
                    ModelState.AddModelError("Password", "Password tidak cocok.");
                    return(View(model));
                }

                //login succes
                //Models.User serializeModel = new Models.User(_user);
                MyPrincipalSerializeModel serializeModel = new MyPrincipalSerializeModel(_user);

                string userData = JsonConvert.SerializeObject(serializeModel);
                FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
                                                                                     serializeModel.username,
                                                                                     DateTime.Now,
                                                                                     DateTime.Now.AddMinutes(120),
                                                                                     true,
                                                                                     userData,
                                                                                     FormsAuthentication.FormsCookiePath);

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

                return(RedirectToAction("Index", "Home"));
                //RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("Username", "Username tidak terdaftar.");
                return(View(model));
            }
        }
Exemplo n.º 2
0
        protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
        {
            HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];

            if (authCookie != null)
            {
                try
                {
                    FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

                    MyPrincipalSerializeModel serializeModel = JsonConvert.DeserializeObject <MyPrincipalSerializeModel>(authTicket.UserData);
                    MyPrincipal newUser = new MyPrincipal(authTicket.Name);

                    newUser.id        = serializeModel.id;
                    newUser.username  = serializeModel.username;
                    newUser.password  = serializeModel.password;
                    newUser.firstname = serializeModel.firstname;
                    newUser.lastname  = serializeModel.lastname;
                    newUser.path_foto = serializeModel.path_foto;

                    newUser.menus    = new List <PrincipalMenu>();
                    newUser.RoleUser = new List <string>();
                    tms_mka_v2.Context.ContextModel dbcontext = new tms_mka_v2.Context.ContextModel();
                    tms_mka_v2.Context.User         dbuser    = dbcontext.User.Where(u => u.Id == newUser.id).FirstOrDefault();

                    foreach (var _menu in dbuser.UserMenus)
                    {
                        PrincipalMenu _menuUser = new PrincipalMenu();
                        _menuUser.MenuName = _menu.Menu.MenuName;
                        _menuUser.Action   = new List <string>();
                        if (_menu.IsCreate)
                        {
                            _menuUser.Action.Add("create");
                        }
                        if (_menu.IsRead)
                        {
                            _menuUser.Action.Add("read");
                        }
                        if (_menu.IsUpdate)
                        {
                            _menuUser.Action.Add("update");
                        }
                        if (_menu.IsDelete)
                        {
                            _menuUser.Action.Add("delete");
                        }
                        if (_menu.IsPrint)
                        {
                            _menuUser.Action.Add("print");
                        }
                        if (_menu.IsProses)
                        {
                            _menuUser.Action.Add("proses");
                        }
                        newUser.menus.Add(_menuUser);
                    }

                    foreach (var _role in dbuser.UserRole)
                    {
                        newUser.RoleUser.Add(_role.Role.RoleName);
                    }

                    HttpContext.Current.User = newUser;
                }
                catch (Exception)
                {
                    //SignOut();
                    //return;
                }
            }
        }