/// <summary> /// Gets information from the data source for a user. Provides an option to update the last-activity date/time stamp for the user. /// </summary> /// <returns> /// A <see cref="T:System.Web.Security.MembershipUser"/> object populated with the specified user's information from the data source. /// </returns> /// <param name="username">The name of the user to get information for. </param><param name="userIsOnline">true to update the last-activity date/time stamp for the user; false to return user information without updating the last-activity date/time stamp for the user. </param> 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 MvcDemoEntities()) { var user = (from u in context.Users.Include(usr => usr.UserRole) where String.Compare(u.Username, 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); } }
/// <summary> /// Gets information from the data source for a user. Provides an option to update the last-activity date/time stamp for the user. /// </summary> /// <returns> /// A <see cref="T:System.Web.Security.MembershipUser"/> object populated with the specified user's information from the data source. /// </returns> /// <param name="username">The name of the user to get information for. </param><param name="userIsOnline">true to update the last-activity date/time stamp for the user; false to return user information without updating the last-activity date/time stamp for the user. </param> 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 MvcDemoEntities()) { var user = (from u in context.Users.Include(usr => usr.UserRole) where String.Compare(u.Username, 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; } }