You could split the Uri.Authority
and check the TLD and Domain Name which should always be the last two elements of the split (assuming it's a valid URL)
Example
Uri uri1 = new Uri("http://test.google.ca/test/test.html");
Uri uri2 = new Uri("http://google.ca/test/test.html");
string[] uri1Parts = uri1.Authority.Split(new char[] { '.' });
string[] uri2Parts = uri2.Authority.Split(new char[] { '.' });
//Check the TLD and the domain
if (uri1Parts[uri1Parts.Length - 1] == uri2Parts[uri2Parts.Length - 1] && uri1Parts[uri1Parts.Length - 2] == uri2Parts[uri2Parts.Length - 2])
{
Console.WriteLine(uri1Parts[uri1Parts.Length - 2] + "." + uri1Parts[uri1Parts.Length - 1]);
}
Edit
If your URIs have ports you'll need to take them into account. Here's a little better version.
public static bool AreSameDomain(Uri uri1, Uri uri2)
{
string uri1Authority = uri1.Authority;
string uri2Authority = uri2.Authority;
//Remove the port if port is specified
if (uri1Authority.IndexOf(':') >= 0)
{
uri1Authority = uri1Authority.Substring(0, uri1Authority.IndexOf(':'));
}
if (uri2Authority.IndexOf(':') >= 0)
{
uri2Authority = uri1Authority.Substring(0, uri2Authority.IndexOf(':'));
}
string[] uri1Parts = uri1Authority.Split(new char[] { '.' });
string[] uri2Parts = uri2Authority.Split(new char[] { '.' });
return (uri1Parts[uri1Parts.Length - 1] == uri2Parts[uri2Parts.Length - 1] //Checks the TLD
&& uri1Parts[uri1Parts.Length - 2] == uri2Parts[uri2Parts.Length - 2]); //Checks the Domain Name
}