tags:

views:

33

answers:

1

I have some url in Uri. How can i check if this url is part of some domain or subdomain.

Example:

URL:

http://www.example.com/something/index.apsx?lala=ddsadasd&dasdas=dsdas&dsdsd=ikpi&das=asd

allowed:

http://www.example.com/something/

not aloowed:

http://www.example.com/some/

if URL is in allowed domain then code continue executing.

+1  A: 
Uri uri = new Uri("http://www.example.com/folder/page.ext?foo=bar");
uri.Segments.Contains("folder"); // true
uri.DnsSafeHost.Split('.').Contains("example"); // true

uri.Segements are:

  • "/"
  • "something/"
  • "index.ext"

and DnsSafeHost.Split('.') are:

  • www
  • example
  • com so you can manipulate with this data as you want.

Edit:

uri.Segments.Any(s => String.Equals(s.Replace("/", String.Empty), "something", StringComparison.OrdinalIgnoreCase));
uri.DnsSafeHost.Split('.').Any(s => String.Equals(s, "example", StringComparison.OrdinalIgnoreCase));
abatishchev
if the target domain you are trying to match is mybank.com, wouldn't this match hahastealyourmoney.com/mybank.com?
Christopherous 5000
an edit for an editbut uri.DnsSafeHost.Split('.').Contains("example") would also match example.stealmymoney.com perhaps just check that DnsSafeHost ends with the proper domain.
Christopherous 5000
@Christopherous: you're right, I edited my post
abatishchev
thx for help. I will try your suggestions.
senzacionale