Ejemplo n.º 1
0
        /// <summary>
        /// The get or set.
        /// </summary>
        /// <param name="httpApplicationState">
        /// The http application state.
        /// </param>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <param name="getValue">
        /// The get value.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// </returns>
        public static T GetOrSet <T>(
            [NotNull] this HttpApplicationStateBase httpApplicationState, [NotNull] string key, [NotNull] Func <T> getValue)
        {
            CodeContracts.VerifyNotNull(httpApplicationState, "httpApplicationState");
            CodeContracts.VerifyNotNull(key, "key");
            CodeContracts.VerifyNotNull(getValue, "getValue");

            var item = httpApplicationState[key];

            if (!Equals(item, default(T)))
            {
                return((T)item);
            }

            try
            {
                httpApplicationState.Lock();

                item = httpApplicationState[key];

                if (Equals(item, default(T)))
                {
                    item = getValue();
                    httpApplicationState[key] = item;
                }
            }
            finally
            {
                httpApplicationState.UnLock();
            }

            return((T)item);
        }
        public static void SetValueOf <T>([NotNull] this HttpApplicationStateBase thisValue, [NotNull] string name, T value)
        {
            if (name.Length == 0)
            {
                throw new ArgumentNullException(nameof(name));
            }
            thisValue.Lock();

            string key = thisValue.AllKeys.FirstOrDefault(k => k.IsSame(name));

            if (value == null || value.Equals(default(T)))
            {
                if (key != null)
                {
                    thisValue.Remove(key);
                }
            }
            else
            {
                key ??= name;
                thisValue[key] = value;
            }

            thisValue.UnLock();
        }
        public static T GetValueOf <T>([NotNull] this HttpApplicationStateBase thisValue, int index, T defaultValue = default(T))
        {
            T result = defaultValue;

            thisValue.Lock();
            if (thisValue.Keys.Count > 0 && index.InRangeRx(0, thisValue.Keys.Count))
            {
                result = thisValue.Get(index).To(result);
            }
            thisValue.UnLock();
            return(result);
        }
Ejemplo n.º 4
0
        public static void Set(this HttpApplicationStateBase state, string name, object data)
        {
            Debug.Assert(state != null);
            Debug.Assert(name != null);

            try
            {
                state.Lock();
                state[name] = data;
            }
            finally
            {
                state.UnLock();
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// The set.
        /// </summary>
        /// <param name="httpApplicationState">
        /// The http application state.
        /// </param>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        public static void Set <T>(
            [NotNull] this HttpApplicationStateBase httpApplicationState, [NotNull] string key, [NotNull] T value)
        {
            CodeContracts.VerifyNotNull(httpApplicationState, "httpApplicationState");
            CodeContracts.VerifyNotNull(key, "key");

            try
            {
                httpApplicationState.Lock();
                httpApplicationState[key] = value;
            }
            finally
            {
                httpApplicationState.UnLock();
            }
        }
        public static T GetValueOf <T>([NotNull] this HttpApplicationStateBase thisValue, [NotNull] string name, T defaultValue = default(T))
        {
            T result = defaultValue;

            if (name.Length == 0)
            {
                return(result);
            }
            thisValue.Lock();

            string key = thisValue.AllKeys.FirstOrDefault(k => k.IsSame(name));

            if (key != null)
            {
                result = thisValue[key].To(result);
            }

            thisValue.UnLock();
            return(result);
        }
Ejemplo n.º 7
0
        public static T Get <T>(this HttpApplicationStateBase state, string name, bool throwIfNotFound = true) where T : class
        {
            Debug.Assert(state != null);
            Debug.Assert(name != null);

            try
            {
                state.Lock();

                if (throwIfNotFound && state[name] == default(T))
                {
                    throw new ArgumentNullException("Cannot find " + name);
                }

                return((T)state[name]);
            }
            finally
            {
                state.UnLock();
            }
        }
        /// <summary>
        /// Configures webpack.
        /// </summary>
        /// <param name="application">The application.</param>
        /// <param name="webpack">The webpack instance.</param>
        /// <exception cref="System.ArgumentNullException">
        /// application
        /// or
        /// webpack
        /// </exception>
        internal static void ConfigureWebpack(this HttpApplicationStateBase application, IWebpack webpack)
        {
            if (application == null)
            {
                throw new ArgumentNullException(nameof(application));
            }
            if (webpack == null)
            {
                throw new ArgumentNullException(nameof(webpack));
            }

            application.Lock();
            try
            {
                application[WebpackApplicationKey] = webpack;
            }
            finally
            {
                application.UnLock();
            }
        }
Ejemplo n.º 9
0
        public static T Get <T>(this HttpApplicationStateBase state, string name, Func <T> createFunc) where T : class
        {
            Debug.Assert(state != null);
            Debug.Assert(name != null);
            Debug.Assert(createFunc != null);

            try
            {
                state.Lock();

                if (state[name] == default(T))
                {
                    state[name] = createFunc();
                }

                return((T)state[name]);
            }
            finally
            {
                state.UnLock();
            }
        }
Ejemplo n.º 10
0
        public ActionResult ValidateUser([Bind(Include = "username,password")] EmployeeLoginViewModel emp_login_view)
        {
            if (ModelState.IsValid)
            {
                string username = emp_login_view.Username;
                string password = emp_login_view.Password;

                Employee employee_logon = db.Employees.Where(emp => emp.Username.ToLower().Equals(username.ToLower())).FirstOrDefault();

                string ipv4    = GetIp();
                Log    new_log = new Log {
                    Attempt_Time = DateTime.Now,
                    Ipv4         = ipv4,
                };

                if (db.Ipv4Blacklist.Any(r => r.Ipv4.Equals(ipv4)))
                {
                    return(Json(new { EnableError = true, ErrorTitle = "Error", ErrorMsg = "Blacklisted IP Address. Please contact system admin." }));
                }

                // Check if valid username
                if (employee_logon != null)
                {
                    // Check if the user is suspended
                    if (employee_logon.Status == 3 || employee_logon.Status == 2)
                    {
                        new_log.successful  = false;
                        new_log.Employee_ID = employee_logon.Employee_ID;
                        db.Logs.Add(new_log);
                        db.SaveChanges();

                        string error_msg = "Your account has been suspended or disabled. Please contact admin";
                        return(Json(new { EnableError = true, ErrorTitle = "Error", ErrorMsg = error_msg }));
                    }
                    // Check if the password is correct
                    else if (Hashing.ValidatePassword(password, employee_logon.Password))
                    {
                        new_log.successful  = true;
                        new_log.Employee_ID = employee_logon.Employee_ID;
                        db.Logs.Add(new_log);
                        db.SaveChanges();

                        HttpApplicationStateBase app_state = HttpContext.Application;

                        FormsAuthentication.SetAuthCookie(new_log.Employee_ID.ToString(), false);
                        // Create a new logon session
                        Session["logon"] = new_log;

                        app_state.Lock();
                        // If the username didnt logon in before
                        if (app_state[new_log.Employee_ID.ToString()] == null)
                        {
                            app_state.Add(new_log.Employee_ID.ToString(), Session.SessionID);
                        }
                        // If the username is logged in and have active session
                        else
                        {
                            string sess_ID = app_state[new_log.Employee_ID.ToString()] as string;
                            if (!sess_ID.Equals(Session.SessionID))
                            {
                                app_state[new_log.Employee_ID.ToString()] = Session.SessionID;
                            }
                        }
                        app_state.UnLock();

                        return(Json(new { EnableSuccess = true, RedirectUrl = "/Employees" }));
                    }
                    // if user is suspended or password is incorrect
                    else
                    {
                        new_log.successful  = false;
                        new_log.Employee_ID = employee_logon.Employee_ID;
                        db.Logs.Add(new_log);
                        db.SaveChanges();
                    }
                }

                // if username is invalid
                else
                {
                    new_log.successful = false;
                    db.Logs.Add(new_log);
                    db.SaveChanges();
                }
            }

            return(Json(new { EnableError = true, ErrorTitle = "Error", ErrorMsg = "Invalid username or password" }));
        }