Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 562 Vote(s) - 3.43 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a URL validator on .Net?

#1
Is there a method to validate URLs in .Net, ASP.Net, or ASP.Net MVC?
Reply

#2
static bool IsValidUri(string urlString) {
try {
new Uri(urlString);
return true;
} catch {
return false;
}
}
Reply

#3
A faster way (probably) than using try/catch functionality would be to use Regex. If you had to validate 1000s of URLs catching the exception multiple times would be slow.

Here's [a link to sample Regex][1]- use Google to find more.


[1]:

[To see links please register here]

Reply

#4
You can use the [`Uri.TryCreate`][1] to validate an URL:

public bool IsValidUri(string uri)
{
Uri validatedUri;
return Uri.TryCreate(uri, UriKind.RelativeOrAbsolute, out validatedUri);
}

The comments suggest that `TryCreate` just moves the exception handling one level down. However, I checked the source code and found that this is not the case. There is no try/catch inside `TryCreate`, it uses a custom parser which should not throw.


[1]:

[To see links please register here]

Reply

#5
You can use Uri.IsWellFormedUriString, no need to create your own function for that:

public static bool IsWellFormedUriString(string uriString, uriKind uriKind);

Where uriKind can be:


UriKind.RelativeOrAbsolute
UriKind.Absolute
UriKind.Relative

For more info see:

[To see links please register here]

Reply

#6
The answers provided thusfar do not check for a scheme, allowing all kinds of unwanted input, which could make you vulnerable for javascript injection (see the comment of TheCloudlessSky).

An URI is just a unique identification of a object. "C:\Test" is a valid URI.

In my project I used the following code:

/// <summary>
/// Validates a URL.
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private bool ValidateUrl(string url)
{
Uri validatedUri;

if (Uri.TryCreate(url, UriKind.Absolute, out Uri validatedUri)) //.NET URI validation.
{
//If true: validatedUri contains a valid Uri. Check for the scheme in addition.
return (validatedUri.Scheme == Uri.UriSchemeHttp || validatedUri.Scheme == Uri.UriSchemeHttps);
}
return false;
}

Define which schemes you will allow and change the code accordingly.
Reply

#7
In case you need the nice code in VB.Net from Arjan

```
'Validates a URL.
Function ValidateUrl(url As String) As Boolean
Dim validatedUri As Uri = Nothing
If (Uri.TryCreate(url, UriKind.Absolute, validatedUri)) Then
Return (validatedUri.Scheme = Uri.UriSchemeHttp Or validatedUri.Scheme = Uri.UriSchemeHttps)
End If
Return False
End Function
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through