public static bool IsRegexMatch(string spec, string target) { spec = spec.Replace(".", "\\."); // Escape special regex character '.'. spec = spec.Replace("?", "\\?"); // Escape special regex character '?'. spec = spec.Replace("*", ".*"); // Convert '*' to its regex equivalent. // Perform a case-insensitive regex match. return Regex.IsMatch(target, string.Format("^{0}$", spec), RegexOptions.IgnoreCase); }
/// <summary> /// Determines whether the user is a member of either of the specified <paramref name="roles"/>. /// </summary> /// <param name="roles">Comma separated list of roles to check.</param> /// <returns>true if the user is a member of either of the specified <paramref name="roles"/>, otherwise false.</returns> public bool IsInRole(string roles) { if (!m_identity.Provider.UserData.IsDefined || !m_identity.Provider.IsUserAuthenticated || m_identity.Provider.UserData.IsDisabled || m_identity.Provider.UserData.IsLockedOut) { // No need to check user roles. return(false); } // Check if user has any one of the specified roles. foreach (string role in roles.Split(',')) { if (m_identity.Provider.UserData.Roles.FirstOrDefault(currentRole => (SecurityProviderUtility.IsRegexMatch(m_identity.Provider.TranslateRole(role.Trim()), currentRole))) != null) { return(true); } } return(false); }