/// <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 teckconfdbEntities()) { var user = (from u in context.AspNetUsers where String.Compare(u.Email, username, StringComparison.OrdinalIgnoreCase) == 0 select u).FirstOrDefault(); if (user != null) userRoles = user.AspNetRoles.Select(r => r.Name).ToArray(); } //Store in cache HttpRuntime.Cache.Insert(cacheKey, userRoles, null, DateTime.Now.AddMinutes(_cacheTimeoutInMinutes), Cache.NoSlidingExpiration); // Return return userRoles.ToArray(); }
public override bool ValidateUser(string email, string password) { if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) return false; using (var context = new teckconfdbEntities()) { var user = (from u in context.AspNetUsers where String.Compare(u.Email, email, StringComparison.OrdinalIgnoreCase) == 0 //&& String.Compare(u.PasswordHash, password, StringComparison.OrdinalIgnoreCase) == 0 select u).FirstOrDefault(); var hasher = new Microsoft.AspNet.Identity.PasswordHasher(); return user != null && hasher.VerifyHashedPassword(user.PasswordHash, password).Equals(PasswordVerificationResult.Success); } }