/// <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> /// Verifies that the specified user name and password exist in the data source. /// </summary> /// <returns> /// true if the specified username and password are valid; otherwise, false. /// </returns> /// <param name="username">The name of the user to validate. </param><param name="password">The password for the specified user. </param> public override bool ValidateUser(string username, string password) { if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return false; using (var context = new MvcDemoEntities()) { var user = (from u in context.Users where String.Compare(u.Username, username, StringComparison.OrdinalIgnoreCase) == 0 && String.Compare(u.Password, password, StringComparison.OrdinalIgnoreCase) == 0 select u).FirstOrDefault(); return user != null; } }
/// <summary> /// Gets a list of the roles that a specified user is in for the configured applicationName. /// </summary> /// <returns> /// A string array containing the names of all the roles that the specified user is in for the configured applicationName. /// </returns> /// <param name="username">The user to return a list of roles for.</param> public override string[] GetRolesForUser(string username) { //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}", username); if (HttpRuntime.Cache[cacheKey] != null) return (string[])HttpRuntime.Cache[cacheKey]; //Get the roles from DB var userRoles = new string[] { }; 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) userRoles = new[]{user.UserRole.UserRoleName}; } //Store in cache HttpRuntime.Cache.Insert(cacheKey, userRoles, null, DateTime.Now.AddMinutes(_cacheTimeoutInMinutes), Cache.NoSlidingExpiration); // Return return userRoles.ToArray(); }