Validate domain name - c#

I have a text box where the user can enter an URL, I would like to validate the user input such as the url is in the same domain as the application (specified in web config).
i would like to use javascript to validate this with regular expressions.
The domain name is "http://www.example.com" and user input that i m expecting is "http://www.example.com/folder".
the domain name is stored in the Webconfig AppSettings sections.

How about an alternative approach? Instead of asking for the full URL:
URL: [http://example.com/foo/bar]
instead ask for a relative URL and only let the user fill in the part after the host:
URL: http://example.com/[foo/bar]
(where [xyz] indicates the input box)

Try
location.href.match(/^http:\/\/(.+?)\//)

Related

how to get url entered by the user with domain?

I want to get the complete URL entered by the user in the browser obviously with the correct domain name. For example, if my domain is www.TestDomain.com and the user enters the correct domain but with some extra parameters like www.TestDomain.com/Test/1124/tres. This new URL causes 404 error so i want to get this URL in the controller action method.
Thanks & Regards

Can I use a format string in a URL to navigate to web page, replacing the specified portion of the URL?

I'm interested in creating some sort of simple C# application takes a user string and passes it into a target portion of the URL. For example, since user query is visible in the page URL DuckDuckGo
Example:
https://duckduckgo.com/?q=web+browsers&ia=web
In this case, the URL shows that I searched for "web browsers". I would like the user to be able to pass any string to the application (via some kind of prompt that appears with the application is launched), and then launch a web browser and navigate to the target URL with the user input inserted into URL where the query is specified. (i.e., https://duckduckgo.com/?q=operating+systems&ia=web), where the user entered the string "operating systems".
So I would like to know which type of C# application to use that can interact with OS (Windows 10) and how to write the code for the the format String and the user prompt. Any guidance would be appreciated.
Your question is very broad so the best that can be done is give a broad answer. You mention "application" and "interact with the OS", so I'm assuming a native application, not a web app. A quick way to pull this off would be to Google for "C# Web Browser Example"; there are plenty of applications out there with well-explained source code that will answer your question:
So I would like to know which type of C# application to use that can
interact with OS (Windows 10)
As for the string replacement, Armine already pointed that out in his previous post. A simple textbox on your form, passed to some parsing code with string replacement, will do the trick for building the URL. The resulting URL is then passed to the web browser control you've used in your C# application; the URL will be one of the properties of the control.
The idea is to take what user typed as a string, and then create another string which will contain the words of that string, separated by the plus character (+)
String what_user_typed=" javascript jquery";
String query=what_user_typed.Replace(" ","+"); // A space represents a new word
String url="https://www.google.com/search?query="+query
After creating the url you can then use a webbrowser to open that url
I have not executed this but think this is what the logic should be.
string input = "operating system";
string destinationURL = $"https://duckduckgo.com/?q={input}&ia=web";
string formattedURL = HttpContext.Current.Server.UrlEncode(destinationURL);
System.Diagnostics.Process.Start(formattedURL);

Change part of the pathname in url using jquery

I am working in an application where I am using localization. For localization both admin and user can change the language. If admin changed recently, that change will be affected to all users.
For Ex, Admin changed the language to en-US, so the url will be http://localhost:81/en-US/form
Then, If user trying to change the language, http://localhost:81/fr-fr/form like this, we need to redirect to http://localhost:81/en-US/form. I need to change that part alone in pathname.
I have tried like,
if (!data.Status)
{
// data.Data contains ("en-US") this part of url
var URL = window.location;
URL.replace("/" + data.Data, "/" + window.location.pathname.split('/')[1]);
}
So, Url changing like http://localhost:81/en-US not http://localhost:81/en-US/form.
If I try this in c# url will be like, url wil be like http://localhost:81/controller/view?languagetag=en-US/something
How can I change this part alone ??
Any idea to achieve this in script or suggest me to do in C#.

Redirect any incoming URLs with diacritics to equivalent with no diacritics

How does one implement URL redirecting for URLs with accents? I'd like all potential URL requests with accents to be rewritten without the accent. This is for client names on a French language site. Users should be typing the name without the accent, but should they not do so then I'd like them to land on the correct page either way.
Example
User enters: www.mysite.fr/client/andré ==> user is redirected towww.mysite.fr/client/andre
The resource identifier (clientName) in the database is stored without the accent. Here's my RouteConfig.cs :
routes.MapRoute(
name: "ClientDetails",
url: "client/{clientName}",
defaults: new { controller = "Client", action = "ClientDetails" }
I understand that there are various methods for removing accents on a string. And yes, this would allow me to remove accents from the received URL parameter within the Controller method. However, I would like to be consistent, so that users always see URLs displayed without accents. Therefore, how do I implement redirecting URLs throughout my web application? Is there an easy way to do this in RouteConfig.cs or Web.config ?
I think you mean Redirect and not Rewrite given that Rewrite would mean that the URL stays the same, and you display the intended content (which I think is what you don't want).
The strategy that I think you want, can be implemented by creating a custom route constraint that matches everything that has an any special chars on it.
Make this custom route to be the first thing to be evaluated in your route table, and map this custom route to a "RedirectController" (that you will create) that takes care of removing the special chars from the URL and redirecting the user to a URL with no special chars
At the beginning of every request, you can make this check to perform a redirect. In your Global.asax.cs file, include the following...
protected void Application_BeginRequest()
{
var originalUri = HttpContext.Current.Request.Url;
var finalUri = new UriBuilder(originalUri);
finalUri.Path = RemoveAccents(
originalUri.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped)
);
// Check if the URL has changed
if (!originalUri.Equals(finalUri.Uri))
{
HttpContext.Current.Response.Redirect(finalUri.Uri.ToString(), true);
HttpContext.Current.Response.End();
}
}
You might also want to add another line for finalUri.Query and UriComponents.Query. For RemoveAccents, I tried the following code but you can use what you'd like:
public string RemoveAccents(string input)
{
return Encoding.UTF8.GetString(Encoding.GetEncoding(1251).GetBytes(input));
}

Generate unique url for every user who registers to your site

Right, so I was working on developing a website(just a beginner though). I have stuck at this issue of generating a unique url for each user who registers on my site.
Now say my site is named, www.mydomainname.com. As of now every user has a profile which has the same url, regardless of the username, www.mydomainname.com/myaccount.aspx.
This is because I extract data for every user based on his username by sql queries. Now I want to remove that myaccount.aspx and add the username of the registered user to the url. This also helps in other users visiting your profile, just like youtube or facebook.
Routing might help me(not sure though), but any ideas or sample code or even shower knowledge of how to achieve this would be of great help.
If you're using the asp.net Membership Provider you can include the user id in a querystring, the user id is a unique GUID value. It would be something like:
Guid userId = (Guid)Membership.GetUser().ProviderUserKey;
string url = string.Format("www.mydomainname.com/myaccount.aspx?user={0}", userId.ToString());
For example, this would give the url:
www.mydomainname.com/myaccount.aspx?user=0CCAFADF-0BAE-4985-8073-1639985740BE
From this URL you can grab the user querystring value with:
string guid = Request.QueryString.Get("user");
You could also implement routing as you have mentioned as one of the options:
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("MyAccount","Account/{username}", "~/myaccount.aspx");
}

Categories