Having the Uri object parse out all unique domains

Using the Uri object to return a list of domain found in the  string

I need to find if Uri (or related .NET object) can return 2 of 3 domains when it parses this string:

https://sso.mydomain.org/openam/?goto=http://evildomain.com/?hacked=mydomain.org:80/ntshome/Home/Index?ClientId=MP";

I would be okay if it returned:

sso.mydomain.org
evildomain.com
mydomain.org:

OR

evildomain.com
mydomain.org

How can I do this with Uri or some other .NET object?

Thanks
newbiewebSr. Software EngineerAsked:
Who is Participating?
 
it_saigeDeveloperCommented:
If all you want is a list of URI's not using LINQ, then you simply need to do something like this:
using System;
using System.Collections.Generic;

namespace EE_Q29086070
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Uri> uris = new List<Uri>();
            var current = default(Uri);
            var data = "https://sso.mydomain.org/openam/?goto=http://evildomain.com/?hacked=mydomain.org:80/ntshome/Home/Index?ClientId=MP";
            foreach (var part in data.Split(new[] { '?' }, StringSplitOptions.RemoveEmptyEntries))
            {
                if (Uri.TryCreate(part, UriKind.RelativeOrAbsolute, out current) && current.IsAbsoluteUri)
                {
                    uris.Add(current);
                }
                else if (part.IndexOf('=') > -1)
                {
                    var pair = part.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                    if (!string.IsNullOrWhiteSpace(pair[1]) && Uri.TryCreate(pair[1], UriKind.RelativeOrAbsolute, out current) && current.IsAbsoluteUri)
                    {
                        uris.Add(current);
                    }
                }
            }

            foreach (var uri in uris)
            {
                Console.WriteLine(uri);
            }
            Console.ReadLine();
        }
    }
}

Open in new window

Which produces the following output -Capture.PNG
-saige-
0
 
aikimarkCommented:
This regex pattern seems to parse your example correctly
(?://|=)(?!https?://)([^/]+)/

Open in new window

0
 
newbiewebSr. Software EngineerAuthor Commented:
I needed to escape the backslashes, but it worked:

(?:\/\/|=)(?!https?:\/\/)([^\/]+)\/

As you can see, the domains are highlighted.

How do I get those three into a list?

RegEx snapshot
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
it_saigeDeveloperCommented:
You can return the parts and then create their own specific uri's; e.g. -
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q29086070
{
    class Program
    {
        static void Main(string[] args)
        {
            var uri = "https://sso.mydomain.org/openam/?goto=http://evildomain.com/?hacked=mydomain.org:80/ntshome/Home/Index?ClientId=MP";
            var ud = new UnifiedDomains(uri);
            Console.WriteLine(ud.Base.AbsoluteUri);
            foreach (var pair in ud.Queried)
            {
                Console.WriteLine($"Query Part: {pair.Key}{Environment.NewLine}Values: {Environment.NewLine}{string.Join(Environment.NewLine, from x in pair.Value select x.AbsoluteUri)}");
            }
            Console.ReadLine();
        }
    }

    class UnifiedDomains
    {
        public Uri Base { get; private set; }
        public Dictionary<string, List<Uri>> Queried { get; private set; }

        public UnifiedDomains(string @base) : this(new Uri(@base)) { }

        public UnifiedDomains(Uri @base)
        {
            Uri @current = default(Uri);
            Base = @base;
            Queried = (from part in @base.Query.Split(new[] { '?' }, StringSplitOptions.RemoveEmptyEntries)
                       let pair = part.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries)
                       where !string.IsNullOrWhiteSpace(pair[1]) && Uri.TryCreate(pair[1], UriKind.RelativeOrAbsolute, out @current) && @current.IsAbsoluteUri
                       select new { Key = pair[0], Value = @current } into paired
                       group paired by paired.Key into groups
                       select new { Key = groups.Key, Value = (from uri in groups select uri.Value).ToList() }).ToDictionary(k => k.Key, v => v.Value);
        }
    }
}

Open in new window

Which would produce the following output -Capture.PNG
-saige-
0
 
newbiewebSr. Software EngineerAuthor Commented:
That code hurts, just looking at it. I am surprised it's so difficult to extract the following:

sso.mydomain.org/openam/
evildomain.com
mydomain.org
         
and your code does not even create that list.

Is there an easier way? One with code I could modify myself?

I do not code LINQ with SQL commands, so it is especially confusing to me.

Thanks.
0
 
newbiewebSr. Software EngineerAuthor Commented:
thank you very much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.