public override string[] GetRolesForUser(string username) { if (!HttpContext.Current.User.Identity.IsAuthenticated) return null; var cacheKey = string.Format("UserRoles_{0}", username); if (HttpRuntime.Cache[cacheKey] != null) return (string[])HttpRuntime.Cache[cacheKey]; var userRoles = new string[] { }; using (var context = new EFDbContext()) { var user = (from u in context.Users where String.Compare(u.Email, username, StringComparison.OrdinalIgnoreCase) == 0 select u).FirstOrDefault(); if (user != null) userRoles = new[]{user.Role}; } HttpRuntime.Cache.Insert(cacheKey, userRoles, null, DateTime.Now.AddMinutes(_cacheTimeoutInMinutes), Cache.NoSlidingExpiration); return userRoles.ToArray(); }
public override bool ValidateUser(string username, string password) { if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return false; using (var context = new EFDbContext()) { var user = (from u in context.Users where String.Compare(u.Email, username, StringComparison.OrdinalIgnoreCase) == 0 && String.Compare(u.Password, password, StringComparison.OrdinalIgnoreCase) == 0 select u).FirstOrDefault(); return user != null; } }
public override MembershipUser GetUser(string username, bool userIsOnline) { var cacheKey = string.Format("UserData_{0}", username); if (HttpRuntime.Cache[cacheKey] != null) return (CustomMembershipUser)HttpRuntime.Cache[cacheKey]; using (var context = new EFDbContext()) { var user = (from u in context.Users where String.Compare(u.Email, username, StringComparison.OrdinalIgnoreCase) == 0 select u).FirstOrDefault(); if (user == null) { return null; } var membershipUser = new CustomMembershipUser(user); //Store in cache HttpRuntime.Cache.Insert(cacheKey, membershipUser, null, DateTime.Now.AddMinutes(_cacheTimeoutInMinutes), Cache.NoSlidingExpiration); return membershipUser; } }