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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
newbiewebSr. Software EngineerAuthor Commented:
thank you very much
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.