コード例 #1
0
ファイル: login.cshtml.cs プロジェクト: Waung/Template-Razor
        /// <summary>
        /// Handle onPost Login AJAX based dengan return value JsonResult
        /// </summary>
        /// <param name="request_parameter"></param>
        /// <param name="returnURL"></param>
        /// <returns></returns>
        public JsonResult OnPost(string request_parameter, string returnURL = null)
        {
            dynamic login_object = JsonConvert.DeserializeObject(request_parameter);
            string  user_name    = login_object["username"];
            string  password     = login_object["password"];
            //Console.WriteLine("user_name>> " + user_name);
            //Console.WriteLine("password >> " + password);
            AppResponseMessage arm = new AppResponseMessage();

            if (!string.IsNullOrWhiteSpace(user_name) && !string.IsNullOrWhiteSpace(password))
            {
                //jika masukan username & password valid
                if (IsValidLogin(user_name, password))
                {
                    //jika username & password dikenali
                    string user_id          = _context.m_user.Where(f => f.user_name == user_name).FirstOrDefault().m_user_id + "";
                    string Role             = _context.m_user.Include(f => f.m_user_group).Where(f => f.user_name == user_name).FirstOrDefault().m_user_group.user_group_name;
                    string user_category_id = _context.m_user.Where(f => f.user_name == user_name).FirstOrDefault().m_user_group_id + "";

                    bool status_aktif = _context.m_user.Where(f => f.user_name == user_name).FirstOrDefault().user_active;
                    if (status_aktif != true)
                    {
                        //jika user tidak aktif
                        arm.fail();
                        arm.message = "user tidak aktif";
                    }
                    else
                    {
                        //jika user valid & aktif
                        var claims = new[] {
                            new Claim(ClaimTypes.Name, user_name),
                            new Claim(ClaimTypes.Role, Role),

                            new Claim("user_id", user_id),
                            new Claim("user_category_id", user_category_id),
                            new Claim("user_name", user_name),
                        };
                        ClaimsIdentity  identity  = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
                        ClaimsPrincipal principal = new ClaimsPrincipal(identity);

                        AuthenticationHttpContextExtensions.SignInAsync(HttpContext, CookieAuthenticationDefaults.AuthenticationScheme, principal);
                        arm.success();
                        arm.message = "login berhasil";
                    }
                }
                else
                {
                    arm.fail();
                    arm.message = "login gagal";
                }
            }
            else
            {
                arm.fail();
                arm.message = "login gagal";
            }
            return(new JsonResult(arm));
        }
コード例 #2
0
        /// <summary>
        /// Post method bisa ditulis tanpa parameter catch
        /// parameter dibaca manual lewat Request.Query untuk QUERY STRING
        /// dan Request.Form untuk FORM BODY POST, termasuk file
        /// </summary>
        /// <returns></returns>
        public JsonResult OnPost()
        {
            gls_model          _context = new gls_model(AppGlobal.get_db_option()); //simplifying context initializer by override
            AppResponseMessage arm      = new AppResponseMessage();                 //inisialisasi ARM sebagai standarisasi respon balik

            //handle kiriman parameter sesuai f >> function, dihandle filternya di ScopePageModel
            if (Request.Query["f"] == "insert_handler")
            {
                string feature_name  = Request.Form["feature_name"];
                int    count_existed = _context.m_feature.Where(e => e.feature_name == feature_name).Count(); //gunakan e >> entity untuk select ef
                if (count_existed > 0)
                {                                                                                             //cek data duplikat
                    arm.fail();
                    arm.message = "Data sudah ada (duplikat)!";
                }
                else
                {
                    //construct object m_feature
                    m_feature m_feature_data = new m_feature {
                        m_feature_group_id = Convert.ToInt32(Request.Form["m_feature_group_id"]),
                        feature_name       = feature_name,
                        feature_sequence   = Convert.ToInt32(Request.Form["feature_sequence"]),
                        feature_url        = Request.Form["feature_url"],
                        feature_icon       = Request.Form["feature_icon"],
                        feature_private    = Convert.ToBoolean(Request.Form["feature_private"]),
                    };
                    _context.m_feature.Add(m_feature_data);    //insert m_feature yg diconstruct
                    arm.success();                             //set success status
                    arm.message = "Data berhasil ditambahkan"; //set success message
                }
            }
            else if (Request.Query["f"] == "edit_handler")
            {
                int    m_feature_id  = Convert.ToInt32(Request.Form["m_feature_id"]);
                string feature_name  = Request.Form["feature_name"];
                int    count_existed = _context.m_feature.Where(e => e.feature_name == feature_name && e.m_feature_id != m_feature_id).Count();
                if (count_existed > 0)
                { //cek data duplikat
                    arm.fail();
                    arm.message = "Data sudah ada (duplikat)!";
                }
                else
                {
                    //construct object m_feature
                    m_feature m_feature_data = new m_feature
                    {
                        m_feature_id       = Convert.ToInt32(Request.Form["m_feature_id"]),
                        m_feature_group_id = Convert.ToInt32(Request.Form["m_feature_group_id"]),
                        feature_name       = feature_name,
                        feature_sequence   = Convert.ToInt32(Request.Form["feature_sequence"]),
                        feature_url        = Request.Form["feature_url"],
                        feature_icon       = Request.Form["feature_icon"],
                        feature_private    = Convert.ToBoolean(Request.Form["feature_private"]),
                    };
                    _context.m_feature.Update(m_feature_data);  //update m_feature yg diconstruct
                    arm.success();                              //set success status
                    arm.message = "Data berhasil diperbaharui"; //set success message
                }
            }
            else if (Request.Query["f"] == "delete_handler")
            {
                int m_feature_id = Convert.ToInt32(Request.Query["id"]);
                var db_row       = _context.m_feature.AsNoTracking().SingleOrDefault(e => e.m_feature_id == m_feature_id);
                if (db_row == null)
                {
                    arm.fail();
                    arm.message = "Data tidak ditemukan!";
                }
                else
                {
                    _context.m_feature.Remove(db_row);
                    arm.success();                         //set success status
                    arm.message = "Data berhasil dihapus"; //set success message
                }
            }

            try
            {
                _context.SaveChanges(); //save changes to database
            }
            catch (Exception ex)
            {
                arm.fail();
                arm.message = ex.Message;
                AppGlobal.console_log("Error Save: ", ex.ToString());
            }
            return(new JsonResult(arm)); //return ARM dg method JsonResult untuk auto serialize ke format JSON
        }