Esempio n. 1
0
        public override string[] GetRolesForUser(string username)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return null;
            }

            // Check cache
            var cacheKey = string.Format("{0}_role", username);

            if (HttpRuntime.Cache[cacheKey] != null)
            {
                return (string[])HttpRuntime.Cache[cacheKey];
            }

            string[] roles = new string[]{};

            using (DatabaseEntities de = new DatabaseEntities())
            {
                roles = (from a in de.Roles
                        join b in de.UserRoles on a.RoleId equals b.RoleId
                        join c in de.Users on b.UserId equals c.UserId
                        where c.Username.Equals(username)
                        select a.RoleName).ToArray<string>();

                if (roles.Count() > 0)
                {
                    HttpRuntime.Cache.Insert(cacheKey, roles, null, DateTime.Now.AddMinutes(_cacheTimeoutInMinute), 
                        Cache.NoSlidingExpiration);
                }
            }

            return roles;
        }
        public override bool ValidateUser(string username, string password)
        {
            using (DatabaseEntities de = new DatabaseEntities())
            {
                var user = de.Users.Where(a=>a.Username.Equals(username) && a.Password.Equals(password)).FirstOrDefault();

                if (user != null)
                {
                    return true;
                }
            }

            return false;
        }