GSF.Security.SecurityProviderUtility.IsRegexMatch C# (CSharp) Method

IsRegexMatch() public static method

Determines if the specified target matches the specified spec.
public static IsRegexMatch ( string spec, string target ) : bool
spec string Spec string that can include wildcards ('*'). For example, *.txt
target string Target string to be compared with the .
return bool
        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);
        }

Usage Example

Esempio n. 1
0
        /// <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);
        }