public static bool ValidateCertificate(string certificateSubject, Model.Enum.Environment environment)
        {
            var environmentName = environment.ToString().ToLower();
            var regexPatternTerminalSpecificCert = _terminalApiCnRegex.Replace(_environmentWildcard, environmentName);
            var regexPatternLegacyCert           = _terminalApiLegacy.Replace(_environmentWildcard, environmentName);
            var subject = certificateSubject.Split(',')
                          .Select(x => x.Split('='))
                          .ToDictionary(x => x[0].Trim(' '), x => x[1]);

            if (subject.ContainsKey("CN"))
            {
                string commonNameValue = subject["CN"];
                if (Regex.Match(commonNameValue, regexPatternTerminalSpecificCert).Success || string.Equals(commonNameValue, regexPatternLegacyCert))
                {
                    return(true);
                }
            }
            return(false);
        }
示例#2
0
        public static bool ValidateCertificate(string certificateCommonName, Model.Enum.Environment environment)
        {
            var enviromentName = environment.ToString().ToLower();
            var patternRegex   = "(?:^|,\\s?)(?:(?<name>[A-Z]+)=(?<val>\"(?:[^\"]|\"\")+\"|[^,]+))+";
            var regex          = new Regex(patternRegex);
            var groupsName     = regex.GetGroupNames();
            var matches        = regex.Matches(certificateCommonName);

            foreach (Match match in matches)
            {
                string groupName = match.Groups["name"].Value;
                if ("CN".Equals(groupName))
                {
                    var commonNameValue = match.Groups["val"].Value;
                    var regexPattern    = _terminalApiCnRegex.Replace(_enviromentWildcard, environment.ToString().ToLower());
                    var legacyCert      = _terminalApiLegacy.Replace(_enviromentWildcard, environment.ToString().ToLower());
                    if (Regex.Match(commonNameValue, regexPattern).Success || string.Equals(commonNameValue, legacyCert))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }