public JsonResult DeleteUserGroup(int UserModeCode)
        {
            //get the result using ignition web service
            var result = UserModels.DeleteUserGroup(UserModeCode);

            if (result == true)
            {
                result = ModuleModels.DeletePermission(UserModeCode);

                if (result == true)
                {
                    result = UserModels.ResetUserByBatch(UserModeCode);

                    if (result == true)
                    {
                        string position     = "";
                        bool   isNAPosition = false;
                        try
                        {
                            position     = UserModels.GetPosition(Session["Username"].ToString());
                            isNAPosition = false;
                        }
                        catch
                        {
                            position     = "N/A";
                            isNAPosition = true;
                        }

                        HttpContext.Session.Add("Position", position);
                        HttpContext.Session.Add("isNAUser", isNAPosition);

                        try
                        {
                            Response.Cookies["Position"].Expires = DateTime.Now.AddDays(-1);
                            Response.Cookies["isNAUser"].Expires = DateTime.Now.AddDays(-1);
                        }
                        catch { }

                        try
                        {
                            HttpCookie cookiePositon = new HttpCookie("Position");
                            cookiePositon.Value   = position;
                            cookiePositon.Expires = DateTime.Now.AddDays(30);
                            Response.Cookies.Add(cookiePositon);

                            HttpCookie cookieisNAUser = new HttpCookie("isNAUser");
                            cookieisNAUser.Value   = isNAPosition.ToString();
                            cookieisNAUser.Expires = DateTime.Now.AddDays(30);
                            Response.Cookies.Add(cookieisNAUser);
                        }
                        catch { }
                    }
                }
            }

            //return the json data
            return(Json(result.ToString(), JsonRequestBehavior.AllowGet));
        }
        public JsonResult RefreshPosition(string username)
        {
            var    result = UserModels.GetPosition(username);
            string json   = result.ToString();

            HttpContext.Session.Add("Position", result);

            try
            {
                Response.Cookies["Position"].Expires = DateTime.Now.AddDays(-1);
            }
            catch { }

            try
            {
                HttpCookie cookiePositon = new HttpCookie("Position");
                cookiePositon.Value   = result;
                cookiePositon.Expires = DateTime.Now.AddDays(30);
                Response.Cookies.Add(cookiePositon);
            }
            catch { }

            return(Json(json, JsonRequestBehavior.AllowGet));
        }
        public JsonResult Attempt(string username, string password)
        {
            try
            {
                //check if model is valid
                if (ModelState.IsValid)
                {
                    //get the result of login using ignition web service
                    string result = HttpHandler.UserLogin(username, password);

                    //handle error
                    if (result == null || result == "")
                    {
                        response.Add("success", false);
                        response.Add("error", true);
                        response.Add("message", "Something went wrong. Please try again later.");
                    }
                    else
                    {
                        if (Convert.ToBoolean(result) == true)
                        {
                            //if the result is true get the user info from AD web service

                            string userType = "";

                            //init the encryptor/decryptor
                            EncryptDecryptPassword e = new EncryptDecryptPassword();

                            //use default credentials for the service
                            HttpClientHandler handler = new HttpClientHandler();
                            handler.UseDefaultCredentials = true;

                            //init the client
                            HttpClient client = new HttpClient(handler);
                            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                            //get the user type from the local db and use it to display the position
                            //we are not using the AD's employee position. instead we will use our application's user type for the position
                            try
                            {
                                userType = UserModels.GetPosition(username);
                            }
                            catch {
                                userType = "N/A";
                            }

                            bool isLoginSuperVision = UserModels.isLoginSuperVision(username);

                            JavaScriptSerializer j = new JavaScriptSerializer();

                            //init the url for the service
                            var url = ConfigurationManager.AppSettings[ConfigurationManager.AppSettings["env"].ToString() + "_api_base_url"].ToString() + "login/userinfo?username="******"&json=true";
                            HttpResponseMessage res = client.GetAsync(url).Result;

                            //if success create session and cookie to store login status
                            if (res.IsSuccessStatusCode)
                            {
                                try
                                {
                                    //create the session and cookie based on the result from AD service (user info)
                                    string  strJson = res.Content.ReadAsStringAsync().Result;
                                    dynamic jObj    = (JObject)JsonConvert.DeserializeObject(strJson);
                                    object  a       = j.Deserialize(strJson, typeof(object));
                                    var     dict    = JsonConvert.DeserializeObject <Dictionary <string, object> >(strJson);

                                    HttpContext.Session.Add("Username", username);
                                    HttpContext.Session.Add("Name", dict["cn"]);
                                    HttpContext.Session.Add("Position", userType);
                                    HttpContext.Session.Add("isLoginSuperVision", isLoginSuperVision);
                                    HttpContext.Session.Add("EmployeeNumber", dict["employeeNumber"].ToString());

                                    string thumbnail = "";

                                    try
                                    {
                                        thumbnail = dict["thumbnailPhoto"].ToString();
                                    }
                                    catch { }

                                    if (thumbnail.ToString() == null || thumbnail == "")
                                    {
                                        byte[] imageBytes        = ReadImageFile(Server.MapPath("~/Content/template/images/default_photo.jpg"));
                                        string imageBase64String = Convert.ToBase64String(imageBytes);
                                        string defaultImage      = imageBase64String;
                                        thumbnail = defaultImage;
                                    }

                                    HttpContext.Session.Add("ThumbnailPhoto", thumbnail);

                                    HttpHandler.UpdateThumbnailPhoto(username, thumbnail);

                                    DateTime now = DateTime.Now;

                                    try
                                    {
                                        HttpCookie cookieThumbnailPhoto = new HttpCookie("ThumbnailPhoto");
                                        cookieThumbnailPhoto.Value   = thumbnail;
                                        cookieThumbnailPhoto.Expires = now.AddDays(30);
                                        Response.Cookies.Add(cookieThumbnailPhoto);

                                        HttpCookie cookieName = new HttpCookie("Name");
                                        cookieName.Value   = dict["cn"].ToString();
                                        cookieName.Expires = now.AddDays(30);
                                        Response.Cookies.Add(cookieName);

                                        HttpCookie cookiePositon = new HttpCookie("Position");
                                        cookiePositon.Value   = userType;
                                        cookiePositon.Expires = now.AddDays(30);
                                        Response.Cookies.Add(cookiePositon);

                                        HttpCookie cookieLoginSupervision = new HttpCookie("isLoginSuperVision");
                                        cookieLoginSupervision.Value   = isLoginSuperVision.ToString();
                                        cookieLoginSupervision.Expires = now.AddDays(30);
                                        Response.Cookies.Add(cookieLoginSupervision);

                                        HttpCookie cookieEmployeeNumber = new HttpCookie("EmployeeNumber");
                                        cookieEmployeeNumber.Value   = dict["employeeNumber"].ToString();
                                        cookieEmployeeNumber.Expires = now.AddDays(30);
                                        Response.Cookies.Add(cookieEmployeeNumber);
                                    }
                                    catch { }
                                }
                                catch
                                {
                                    //handle users not in the AD (standalone user for the application)

                                    //get the default thumbnail photo
                                    byte[] imageBytes        = ReadImageFile(Server.MapPath("~/Content/template/images/default_photo.jpg"));
                                    string imageBase64String = Convert.ToBase64String(imageBytes);

                                    string defaultImage = imageBase64String;

                                    //create session and cookie
                                    HttpContext.Session.Add("Username", username);
                                    HttpContext.Session.Add("Name", username);
                                    HttpContext.Session.Add("Position", userType);
                                    HttpContext.Session.Add("isLoginSuperVision", isLoginSuperVision);
                                    HttpContext.Session.Add("EmployeeNumber", "");
                                    HttpContext.Session.Add("ThumbnailPhoto", defaultImage);

                                    HttpHandler.UpdateThumbnailPhoto(username, defaultImage);

                                    DateTime now = DateTime.Now;

                                    try
                                    {
                                        HttpCookie cookieThumbnailPhoto = new HttpCookie("ThumbnailPhoto");
                                        cookieThumbnailPhoto.Value   = defaultImage;
                                        cookieThumbnailPhoto.Expires = now.AddDays(30);
                                        Response.Cookies.Add(cookieThumbnailPhoto);

                                        HttpCookie cookieName = new HttpCookie("Name");
                                        cookieName.Value   = username;
                                        cookieName.Expires = now.AddDays(30);
                                        Response.Cookies.Add(cookieName);

                                        HttpCookie cookiePosition = new HttpCookie("Position");
                                        cookiePosition.Value   = userType;
                                        cookiePosition.Expires = now.AddDays(30);
                                        Response.Cookies.Add(cookiePosition);

                                        HttpCookie cookieLoginSupervision = new HttpCookie("isLoginSuperVision");
                                        cookieLoginSupervision.Value   = isLoginSuperVision.ToString();
                                        cookieLoginSupervision.Expires = now.AddDays(30);
                                        Response.Cookies.Add(cookieLoginSupervision);

                                        HttpCookie cookieEmployeeNumber = new HttpCookie("EmployeeNumber");
                                        cookieEmployeeNumber.Value   = "";
                                        cookieEmployeeNumber.Expires = now.AddDays(30);
                                        Response.Cookies.Add(cookieEmployeeNumber);
                                    }
                                    catch {
                                    }
                                }

                                r.Add("success", true);
                                r.Add("error", false);
                            }

                            var check = r["success"];
                            if (check.ToString() == "True")
                            {
                                response.Add("message", "Login Successful");
                                response.Add("success", true);
                                response.Add("error", false);
                            }
                            else
                            {
                                throw new Exception("Login failed!");
                            }
                        }
                        else
                        {
                            response.Add("success", false);
                            response.Add("error", true);
                            response.Add("message", "Invalid username and/or password.");
                        }
                    }
                }
                else
                {
                    throw new Exception("Login failed!");
                }
            }
            catch (Exception e)
            {
                response.Add("success", false);
                response.Add("error", true);
                response.Add("message", e.ToString());
            }


            //return the json response
            return(Json(response, JsonRequestBehavior.AllowGet));
        }