private static List<CredentialBasic> GetCredentialList(string filter=null)
        {
            ProcessStartInfo start = new ProcessStartInfo();
            start.FileName = "cmdkey.EXE";
            start.Arguments = @"/list";
            start.RedirectStandardOutput = true;
            start.UseShellExecute = false;
            List<CredentialBasic> credList = new List<CredentialBasic>();

            try
            {
                string result;

                using (Process process = Process.Start(start))
                {
                    using (StreamReader reader = process.StandardOutput)
                    {
                        result = reader.ReadToEnd();
                    }
                }
                var matches = Regex.Matches(result, @"Target:\s*(LegacyGeneric|Domain)\s*:\s*(.*)", RegexOptions.Multiline);

                foreach (Match match in matches)
                {
                    var cred = new CredentialBasic();
                    if (match.Groups != null && match.Groups.Count >= 2)
                    {
                        string credType = match.Groups[1].ToString().Trim();
                        switch (credType)
                        {
                            case "LegacyGeneric":
                                cred.CredType = CRED_TYPE.CRED_TYPE_GENERIC;
                                break;
                            case "Domain":
                                cred.CredType = CRED_TYPE.CRED_TYPE_DOMAIN_PASSWORD;
                                break;
                            default:
                                continue;
                        }
                        Match uriMatch = Regex.Match(match.Groups[2].ToString(), @"target=(.*)");
                        if (uriMatch.Groups != null && uriMatch.Groups.Count >= 2)
                        {
                            cred.Uri = uriMatch.Groups[1].ToString().Trim();
                            credList.Add(cred);
                        }

                    }

                }
            }
            catch
            {
                //Somehow this failed
                return null;
            }

            if (filter != null)
            {
                var filteredList = from cred in credList
                                   where cred.Uri.Contains(filter) select cred;
                return filteredList.ToList();
            }
            return credList;
        }
Exemplo n.º 2
0
        private static List <CredentialBasic> GetCredentialList(string filter = null)
        {
            ProcessStartInfo start = new ProcessStartInfo();

            start.FileName  = "cmdkey.EXE";
            start.Arguments = @"/list";
            start.RedirectStandardOutput = true;
            start.UseShellExecute        = false;
            List <CredentialBasic> credList = new List <CredentialBasic>();

            try
            {
                string result;

                using (Process process = Process.Start(start))
                {
                    using (StreamReader reader = process.StandardOutput)
                    {
                        result = reader.ReadToEnd();
                    }
                }
                var matches = Regex.Matches(result, @"Target:\s*(LegacyGeneric|Domain)\s*:\s*(.*)", RegexOptions.Multiline);

                foreach (Match match in matches)
                {
                    var cred = new CredentialBasic();
                    if (match.Groups != null && match.Groups.Count >= 2)
                    {
                        string credType = match.Groups[1].ToString().Trim();
                        switch (credType)
                        {
                        case "LegacyGeneric":
                            cred.CredType = CRED_TYPE.CRED_TYPE_GENERIC;
                            break;

                        case "Domain":
                            cred.CredType = CRED_TYPE.CRED_TYPE_DOMAIN_PASSWORD;
                            break;

                        default:
                            continue;
                        }
                        Match uriMatch = Regex.Match(match.Groups[2].ToString(), @"target=(.*)");
                        if (uriMatch.Groups != null && uriMatch.Groups.Count >= 2)
                        {
                            cred.Uri = uriMatch.Groups[1].ToString().Trim();
                            credList.Add(cred);
                        }
                    }
                }
            }
            catch
            {
                //Somehow this failed
                return(null);
            }


            if (filter != null)
            {
                var filteredList = from cred in credList
                                   where cred.Uri.Contains(filter) select cred;
                return(filteredList.ToList());
            }
            return(credList);
        }