Website visitor location in ASP.NET - c#

My ASP.NET website needs to know the city and country of the visiting user. I have tried following code:
string ip = HttpContext.Current.Request.UserHostAddress.ToString();
And then making a web request to http://api.hostip.info/get_html.php?ip=ip
This method returns me correct user location in some cases, but when user's ISP has hosted server in other country or city, then instead of giving me user's current city (from where he is browsing the website) it gives me the server hosted location.
How can I get the user's location regardless of where his ISP has hosted the server? Like Google does in Google Maps (where browser asks for permission to share location). Or any other better way?

The answer to your problems
See msdn code sample

Related

Internet Information Service(IIS) Manager issue

I am publishing my site in IIS and my WebDav folder is configured in other PC of IIS.
I want to get that website name using some c# code.
How can I get the site name of my WebDav folder in my published web site (ex: Default Web Site) using c# code?
So you want to figure out what Site Name(s) exist on an IIS instance from the IP address?
take a look at this artice:
http://cstruter.com/blog/306
and where you see this line of code:
foreach (Website site in GetSites("IIS://localhost/W3SVC"))
put the IP address where you see localhost. if the ip address was 123.123.123.123 then it would look like this:
foreach (Website site in GetSites("IIS://123.123.123.123/W3SVC"))
You will need adequate permissions to enumerate the Sites! (see the comments below the article for some notes on this)

Obtain IP Address of the User of an IFrame

I am hosting several IFrames on a 3rd party web site. The 3rd party site is the middle man to allow user access to a web site that is primarily for internal use by another company.
What I would like to do is verify the user's IP Address against a range of valid IP's for the 3rd party (middle man) web site. If it falls in the range allow the user to access the IFrame, if not - ACCESS DENIED!
Anyways, there are a ton of examples online to get the Request.UserHostAddress, but this brings back my IP Address.
My question is: how do I get the user's IP Address whom is accessing the IFrame to assure the request is coming from the 3rd party site?
So far I have tried ServerVariables and UserHostAddress. They both return ::1. I am running the site that hosts the IFrames locally and accessing those through the 3rd party site which is hosted on a server.
UPDATE
I have finally gotten around to updating everyone. Trying to do this by code is not a viable solution. But, I believe there is a solution for this through authenticating the IP Address with IIS. In code without implementing a very hacky solution we will only be able to obtain the user's IP Address.
However, by using IIS you can verify the 3rd party's address. This post from SO demonstrates how to do it. The post does not fit my case, but it does show you how to authenticate IP ranges.
Direct different IP's to different pages on IIS7
If I am successful I will specify that this is a solution for my problem.
I think you have the wrong idea of what should be happening.
As I understand it the situation is this:
You have Site A which has Iframes that have content from Site B in them. When you go to Site B directly the IP address you get is for your computer. What you are expecting is that when you go to Site A that the IP address that Site B receives will be that of Site A. This is incorrect.
When you view Site A you will download the page that will contain the IFrame HTML which will tell it what content to put in that frame by having a src="url" type syntax. At this point your browser (ie on your computer) will request the content from Site B and in doing so Site B will receive the IP of your computer again.
One thing that does what you want is the Referrer header (https://en.wikipedia.org/wiki/HTTP_referer). This is a way for a request to say what page told you to access that page. For example if you click to a link on Page C to Page D then Page D may receive a referer header saying that Page C referred it. The important part of that is that it may send it. It is often considered a security risk (especially between domains) so may not be sent in all cases or may be stripped by security tools and most importantly can be faked so it is not suitable for security purposes.
In general it is not easy to determine when serving a page that it is being embedded in a specific page's iframe.
The only way I can think of offhand to do this is when generating the iframe and its src target is to embed some kind of cryptographically signed token in that Site B can trust has been put there by site A. The token would of course have to have some kind of expiry to it and similar things to prevent a malicious user getting unfettered access once they have a token and some way to prevent replay attacks, etc.
In general the best bet would be to just use security on your Site B (eg username and password) and if somebody unintended gets to see it they don't have password details so they don't get anywhere.

How to redirect based on visitor's IP address

I need to create a landing page that will automatically redirect a visitor to the correct country page of a website by working out their location from their IP address.
This is an EPiServer 4 site. How would you go about doing this and does EPiServer have this functionality built in?
You'll need to use a Geolocation Service provider, of which there are many.
However, wouldn't it be better to let the user choose their location, lest they are visiting through a proxy, or temporarily on vacation or something?
You can use splitterhq
Automatically redirect users to different links based on what country, region or city they are in to show targeted content and increase your conversion rates.

How can I create a subdomain in asp.net?

How can I create a subdomain in an asp.net C# application? I am working with an asp.net portal. In that portal I need to be able to create the subdomain at runtime. The ftp for that subdomain also needs to be the same as the main domain ftp.
There are a handful of working parts here. It sounds like the requirement is that this all happen at runtime. I am sure a lot of web hosting providers are deep into these sets of problems and likely have custom solutions.
DNS: you'll need to have an API open at the DNS host. If it's a LAN environment, you'll have to check your DNS provider on whether they have APIs exposed. (i.e. Windows Server or whatever your network operating system is). If this is a public facing site that needs to be accessed via the internet, you'll have to get those APIs from the DNS registrar. Are the subdomains to be permanent?
IIS: You'll have to setup wildcard mapping, (i.e. *.mydomain.net). When an HTTP request comes in, logic within the ASP.NET page can determine the subdomain that you're working with. You could extract the subdomain, perform a lookup on that subdomain to get more information (CustomerID, names, valid visitor IP address ranges, whathaveyou).
FTP: if you require secure logins for that subdomain, you'd have to have created AD or local machine accounts for those users. If you want anonymous access, that's OK too. There are some scripting options that you'll have to look at to have that FTP folder setup as well. That is assuming you're using IIS's FTP module. If you are using a 3rd party FTP application, that complicates the scenario even further. Their APIs would have to be investigated.
Active Directory: If you are requiring secure access for each subdomain, you'll have to create AD users and/or groups. You can certainly script the creation of users into groups. Perhaps the users will be local to your web server. That doesn't scale well if you want to add another web server, but it's certainly do-able.
subdomain's are controlled by your dns settings. Beyond that I can't understand your question.
I think the best way is to have a wildcard DNS entry - i.e. *.domain.com that points to the domain. And then use a rewriting tool, like helicontech.com, to rewrite the 1st part of the subdomain to tell your app what you're looking at.
This would use regular expressions to pass the subdomain to your app:
RewriteCond Host: (mysub).domain.com
RewriteRule (.*) /$2?subdomain=$1 [I,L,U]
There are two halves to this:
A) Changing your DNS Settings:
This varies based on your host and whatnot. Contact them and ask if you can't figure it out.
B) Changing your host settings:
This varies based on your server. I assume you're using some version of IIS.
In IIS6, you can right click a web site, select the properties page, go to the web site tab, select advanced in web site identification, and start adding bindings (Generally you'll use default or the ip address for IP Address, 80 for TCP Port (unless SSL), and the site for the host header file (e.g. www.example.com or subdomain.example.com).
In IIS7, you right click on a website and select edit bindings, then click "Add". IIS7's add screen is pretty self explanatory.
I think he wants to created a subdomain from code, using an API probably that needs to be provided by his webhost folks. Like his own DNS manager app. If I interpreted your question wrong, then I guess you probably oughta reiterate a bit more.

How to get IP numbers of guest of our web page

I have web site (coded in ASP.NET,c#) and I want to show IP based statistic information of it's pages. I don't want to use other analysis tools (like Google Analytics) .
In your ASP.NET page you can use Request.UserHostAddress property to get the user's IP address.
Also, you can use IIS logs (by default in C:\Windows\System32\LogFiles\W3SVCxxxxx....)
HttpContext.Current.Request.UserHostAddress
will return the user's IP address as a string.

Categories

Resources