Exemplo n.º 1
0
        public override string[] GetRolesForUser(string userEmail)
        {
            ////-----------------ket noi voi database de lay quyen cua tai khoan-----------------
            //Return if the user is not authenticated
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                return null;

            //Return if present in Cache
            var cacheKey = string.Format("UserRoles_{0}", userEmail);
            if (HttpRuntime.Cache[cacheKey] != null)
                return (string[])HttpRuntime.Cache[cacheKey];

            //Get the roles from DB
            var userRoles = new string[] { };
            using (sanslabEntities context = new sanslabEntities())
            {
                var user = (from u in context.users
                            where String.Compare(u.email, userEmail, StringComparison.OrdinalIgnoreCase) == 0
                            select u).FirstOrDefault();

                if (user != null)
                    userRoles = new[] { user.chuc_vu.mo_ta };
            }

            //Store in cache
            HttpRuntime.Cache.Insert(cacheKey, userRoles, null, DateTime.Now.AddMinutes(_cacheTimeoutInMinutes), Cache.NoSlidingExpiration);

            // Return
            return userRoles.ToArray();
        }
Exemplo n.º 2
0
        public override bool IsUserInRole(string userEmail, string roleName)
        {
            using (sanslabEntities db = new sanslabEntities())
            {
                user usr = db.users.FirstOrDefault(u => u.email.Equals(userEmail, StringComparison.CurrentCultureIgnoreCase));

                var roles = from r in db.chuc_vu where r.id == usr.id_chuc_vu select r.mo_ta;
                if (usr != null)
                    return roles.Any(r => r.Equals(roleName, StringComparison.CurrentCultureIgnoreCase));
                else
                    return false;
            }
        }