I have a form that users enters there website. Problem is some users put their email address in which I do not want. I want a way to check if the url is well structured. e.g. no #, must have a root domain. www subdomains are optional. I am unable to find this anywhere.
I have tried this code
if (!Uri.TryCreate("http://" + websiteurl, UriKind.Absolute, out uri) || null == uri)
returning false on error but my problem is that it still validates without a root domain e.g. I can put in
http://websitename
and validates fine which I do not want. It does return false when I have put in
http://websitename#.
Is there a way I can overcome this problem? also I added
http:// in the passthrough value because the url never validates.
You can use:
Uri.IsWellFormedUriString(inputUrl, UriKind.RelativeOrAbsolute)
Depending on your performance needs, maybe issuing a quick HttpWebRequest for the website url they give and verifying that you get back a success response might be a good option.
You could try with a regular expression.
Uri.IsWellFormattedUriString won't solve the problem here, which includes the ability to distinguish a valid Url from an email address. Both are well formatted Uris.
Use a regular expression. Here's one from the MS forums using C#:
Url validation with Regular Expression
But you should really validate this before it gets sent to the server. If you use the Peter Blum validators, he's already done the work for you.
Peter Blum's Validators
Or if you want to put in your own JavaScript file, check out this StackOverflow thread.
Url Validation using jQuery
Related
So I have a google shortened Url, and once I click on it and hit my controller, I want to be able to see what the original goo.gl url was before it got resolved. How on earth do I do this?
I've tried Request.UrlReferrer.AboluteUri and System.Web.HttpContext.Current.Request.Url.AbsoluteUri but neither seem to work. They all simply return the resolved absolute uri. Any help on this would be greatly appreciated.
Here's an example of the shortened URL - http://goo.gl/WSrJ6
This would then take the user (in testing at least) to localhost:81/college/events/details/23
So basically, when I hit the Details Controller, how do I get the original shortened url back?
Ok, this feels like more of a workaround but...
I don't see any way to resolve the shortened url from the goog.gl service. However, you could send a web request to goog.gl that only uses the HEAD HTTP verb using the shortened url.
Then, in the response to the HEAD request, the location header will be the original url (because it will send back a redirect request 301).
You can check out the response by using this tool. Put in your shortened url and then choose the HEAD verb before posting.
https://developers.google.com/url-shortener/v1/getting_started
You do a get request with the following url and you get a Json with the long url
https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/WSrJ6
Any normally configured browser sends the header HTTP_REFERER.
Doesn't a simple Request.UrlReferrer work? Or, something like HttpContext.Current.Request.ServerVariables["HTTP_REFERER"] or ServerVariables["HTTP_REFERER"]?
That, depending on where you are coding, but the point is to grab the header from the request.
HTTP_REFERER should always contain the previous (referer) url. I don't see why you're getting the resolved url there, unless it's a second redirection (e.g. your shortener pointed to http://server.com and your web server is configured to redirect missing www to http://www.server.com).
HTH
Francisco
I have a problem authenticating my user against Active Directory. I am trying to authenticate my user via PrincipalContext.
My issue is that when user password contains non-ASCII character validation fails even with the correct credentials. But I have this problem only on my prod environment. It works just fine on UAT and development environments.
How can I resolve this issue? Is there any setting for AD has anything to do with this?
try to change the password encoding to UTF-8
It is a shot in the dark, but hear me out. This is why I asked if this was a web project. I had a similar problem with identical symptoms. A user came to me saying he couldn't login in my website. Turns out he had "special" characters in his password. It didn't make sense, but after disabling custom errors I realized the error was due to ASP.NET Request Validation: http://msdn.microsoft.com/en-us/library/hh882339%28v=vs.100%29.aspx
Request validation is a feature in ASP.NET that examines an HTTP
request and determines whether it contains potentially dangerous
content. In this context, potentially dangerous content is any HTML
markup or JavaScript code in the body, header, query string, or
cookies of the request. ASP.NET performs this check because markup or
code found in the URL query string, cookies, or posted form values
might have been added to the request for malicious purposes.
Pretty much everything that looked like a tag got flagged and runtime threw an exception way before my code had a chance to validate user's password.
Hope this helps!
I am workign with OAuth2 and and I am calling Google API. Google returns results after call is complete and I am supposed to read from the query parameters. Now, the kind of URL that Google Returns is weird and it has anchor # in it exactly where there should be a ?
and URL looks something like
http://localhost.contestfactory.com/enduser/#state=MDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAw&access_token=ya29.AHES6ZTjWwx7hHO4WnmfQ_lwJSpATCqA_DUZCC_ZIjdyPWA96OS0EN0&token_type=Bearer&expires_in=3600
Because of # in the URL my C# code fails to read beyond #. Is there anyway I can deal this problem in C#?
The part after the # is called the URL fragment (or hash).
It is never sent to the server.
what ur saying looks like hash fragment.. and it is never sen tto the server. what u can do instead make a JS script that changes all the "#" to "?" that way they will be treated as query string and will be sent to the server
I'm searching for a c# function that takes a Url as a parameter and returns all the inbound url related to that url.
You can "download" webpages using the WebClient class:
String url = "http://www.google.com";
WebClient client = new WebClient();
String source = client.DownloadString(url);
Then you need to search all URLS. I'd love to write a RegEx for you, if you'd put effort in finding the answer which you didn't, apparently.
Writing one of those Regular Expressions is rather hard because there are so many different things you have to match:
Relative URL's
Absolute URL's
IP's
You have to consider the base tag
Only if they're in specific tags (a, img, link, script, and on and on)
Good luck with that
From your description you want to find "inbound"? url's to a URL. If that is the case you would need to connect to an API to retrieve that information. I don't think Google has one but I do know they exist.
i want to check if a url is from youtube.com website or the mobile version of the site.
is there a robust way to do this?
checking the url contains "youtube.com" does not seem good to me.
whats the proper way to do it?
Use the Uri class to parse the URL and compare to the Host property.
Uri uri = new Uri(myURL);
return uri.Host.Equals("youtube.com", StringComparison.InvariantCultureIgnoreCase)
I don't know a foolproof way to make sure it's youtube.com coming in, but checking REFERER is really not all that solid: the page linking to you can fake its referer header any time it wants to:
http://www.stardrifter.org/refcontrol/
It'll be interesting to see how the security gurus answer this question.
-- pete