As far as I understand, Request.IsLocal returns true in two scenarios. If the IP address of the request originator is 127.0.0.1, or if the IP address of the request is the same as the server's IP address.
I'm using this on a page which is regularly called by a cron task (using an absolute URL). Unfortunately this cron job is run on the same server as the page, meaning that IsLocal always returns true.
Ideally, I need a function which will just return true if I'm on localhost. How can I do this in a ASP.NET MVC environment?
You could look at the Request.Url property in MVC, but you need to check for 127.0.0.1, localhost and ::1 at least. There's also quite a lot you can do with UrlRewrite if all you want to do is request access. You don't say what you want to do if it returns true/false but assuming that's one of the things you want to do, have a look at UrlRewrite. There's a bunch of useful information in this post too;
How to limit page access only to localhost?
Related
I know this is quite easy to get URL in many ways but I have unexpected issues when querying website by DNS, which redirects to a non-default port.
There is a web app working on port 7000 which is accessible by server name or dns:
http://MyServer:7000
http://MyApp.intranet.net (maps MyServer on port 7000)
This is how users see them in the browser.
So now, how to get exactly the same URL link like in a browser.
HttpContext.Current.Request.Url.AbsoluteUri returns respectively:
http://MyServer:7000
http://MyApp.intranet.net:7000 (Port was added, this address doesn't exist!!!)
HttpContext.Current.Request.Url.OriginalString:
http://MyServer:7000
http://MyApp.intranet.net:7000 (the same like above)
HttpContext.Current.Request.Url.Authority:
MyServer:7000
MyApp.intranet.net:7000 (the same like above)
HttpContext.Current.Request.Url.Host:
MyServer (no port information, returns error)
MyApp.intranet.net (that's fine)
I didn't find any property which returns both addresses correctly, as browser user has.
And I don't want to hardcode anything or send this from the client-side.
Thanks for any advice.
We discovered that Request.UserHostAddress is not available in ASP.NET Core (1.0 RC1), as well as Request.ServerVariables["REMOTE_ADDR"], which returns essentially the same.
Now we found HttpContext.Connection.RemoteIpAddress or HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress to be the replacement, but is it really a 1:1 replacement for the functionality?
What I mean: Request.ServerVariables["REMOTE_ADDR"] only returns the direct IP address of a caller, not a forwarded client address (therefore you have to get "X-FORWARDED-FOR"). And that's what we need in our scenario: the direct caller address, not the forwarded one. But we couldn't find any info if HttpContext.Connection.RemoteIpAddress in ASP.NET Core is the right choice for us (and if not, what's the alternative?).
You only get X-Forwarded-For replacing it when you have plumbed that middleware into the pipeline. So to enable that use app.UseOverrideHeaders().
Without it RemoteIpAddress is going to be 127.0.0.1 in most configurations, because you will have IIS in front of Kestrel.
If I have a domain which is:
(1) http://www.myownsite.com
And I have another domain that has a link to it:
(2) http://www.pointingtomyownsite.com
Is there a way to find out whether (1) is opened through a redirect from (2) in ASP.NET MVC?
It depends on what you mean by "redirect". If you mean a CNAME DNS entry (where one hostname appears in the browser even though the page is served from another host, and which many people incorrectly call a DNS redirect), then Senad's answer is correct, Request.Url.Host will work. (This is not a redirect BTW).
A real redirect will change the hostname shown in the browser window, and you can get the URL of the previous page using HttpRequest.UrlReferrer. This works for both redirects and links. Although the Referer HTTP header this property exposes is not technically required and may be NULL, in practice it will be sent by all the browsers you should care about except for possibly if the URL was entered directly on a new tab.
Yes,
var hostname = requestContext.HttpContext.Request.Url.Host
e.g. for mail.google.com would it return google.com or mail.google.com?
I can't actually test it myself
It does include subdomain (e.g. mail.google.com)
You can save yourself from the headache of waiting for answers by reading documentation on Msdn.
A String that contains the host name. This is usually the DNS host name or IP address of the server.
If the requested DNS record is a subdomain, that's the record it will return. Subdomains are still there own records in a zone file, so its not going to return just the root domain because that's not the same record, nor request.
I was also curious as to why you couldn't test this, but if its because of the lack of an internet connection (maybe you're posting from mobile I don't know) you can add your own records to the Windows HOSTS file and test locally.
I am trying to get the IP address of the request coming in from the httpheader. If value is x do something, if not do somthing else. Can anyone help me out?
Does this help? it seems pretty straight forward.
From the link, there are a few different ways:
HttpContext.Current.Request.UserHostAddress;
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
You might want to look here. The HTTP standards do not define the source address as part of the protocol. Infact HTTP does not directly relate to IPv4 /IPv6 at all.
Generally, a webserver will easily be able to tell the network address of the other end of the socket ( the end where the web browser is ). It does not use HTTP to do this, rather it will use features of the OS's networking stack.
In the case of ASP.NET you should be able to find the IP address where the request came from. You might try looking at System.Web.HttpRequest.UserHostAddress