Equivalent for Request.UserHostName in OWIN/Nancy - c#

I am trying to return the users host name (not address) in owin. I am running nancy on top of this. I know I can use this.Request.UserHostAddress to get the IP but I need the name. I have looked through the API's goggled and I ma struggling to find this information.
The only way I can determine to do this is use the ASP.NET HttpContext.Current.Request.UserHostName but this won't work when we self host owin and writing code to determine this information depending on how OWIN is hosted seems to defeat the object of OWIN.

From an IP address, you can use Dns.GetHostEntry (or moral equivalents) to get a IPHostEntry object that has a HostName property.
That's all that UserHostName was doing for you anyway (a DNS lookup):
Gets the DNS name of the remote client.

Related

How to get client domain name or client IP Address which is trying to call web api in web api

I'm doing one project in Web API, So I want to get the client domain name or client IP Address which is trying to call my Web API.
Is there any way to get the same.
I'm waiting for your valuable response.
You can get the IPv6 address in the current context with this:
string IPv6 = HttpContext.Current.Request.UserHostAddress;
HttpContext.Request.Headers.Origin
The above will get you the domain name. For IP address, this post should give you the details.
Domain name is included with each request lands on your server.
If you open network tab in devtools of your browser (chrome for example) check request headers, and you will find the key-value origin scheme://sub.domain.extension as in https://mail.google.com. In Asp.Net Core you can find it in HttpContext.Request.Headers.Origin as an object property.
HttpContext.Request.Headers it self is a key-value-pair dictionary, as such, you can access the origin header, or any other header for that matter, using HttpContext.Request.Headers["origin"].

Configure ASP.NET Core to work with proxy servers and load balancers

I have followed all steps explained on this Microsoft's documents page to be able to obtain remote client's IP address in an IIS-hosted app by calling HttpContext.Connection.RemoteIpAddress , but I keep getting the loopback IP address of ::1. There is only one weird scenario that gets me the remote client's IP address and that's by the service configuration code below where ForwardedForHeaderName is initialized by X-Forwarded-For-Custom-Header-Name which does not make any sense to me!
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardLimit = 2;
options.ForwardedForHeaderName = "X-Forwarded-For-Custom-Header-Name";
});
The full source code is found in this github repo and I'd like to know what exact change must be done to obtain the remote IP address successfully by removing "X-Forwarded-For-Custom-Header-Name" and why such a string gets me the IP address!
The idea is that X-Forwarded-For-Custom-Header-Name is meant to be replaced with a custom header name in case your proxy / load balancer doesn't use the standard X-Forwarded-For header but something different.
While X-Forwarded-For is the de-facto standard here, some proxies / load balancers use another header. In this case you would set it to the value used by your it, for examle X-Real-IP.
In your case, you will have to look at which headers are used in your setup and then configure your application accordingly.
When hosting in IIS using the default hosting model (dotnet publish should generate the appropriate web.config file), the forwarding is already set up and handled by the IIS middleware.
Unless you expect proxy to forward the original IP address in a header that is different from the usual X-Forwarded-For you don't have to specify ForwardedForHeaderName, instead configure it as:
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});

ASP.Net Core Detect real client IP

I've got an ASP.NET Core app hosting on IIS, the server with which is behind the router. I need to detect real clients IPs. From the local network context.HttpContext.Connection.RemoteIpAddress returns correct IP, but from the outside it is always 127.0.0.1.
Also I tried to look at X-Original-For header which locally gives 127.0.0.1 and nothing form the outside.
I tried UseForwardedHeaders (https://stackoverflow.com/a/41450563/4801505) middleware but as I got it the middleware is already used by UseIISIntegration (https://github.com/aspnet/Docs/issues/2384) so there is no sense using it twice.
So, can I achieve the thing at all in my circumstances? Maybe some IIS config required?
Use the external link to get ip address.
I end up using
var httpClient = new HttpClient();
var ip = await httpClient.GetStringAsync("https://api.ipify.org");
If you are using any language other than C# refer to website ipify

DIRECT replacement of UserHostAddress in ASP.NET Core?

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.

Getting IIS Websites' Domain names

I've created a web application to list all websites on IIS with their names, ports and physical paths. it's also supposed to get the IIS websites domain names. the application is installed on IIS and all functions work great except for returning domain names.
I wrote the below code and it returned "localhost:8183" for all websites. 8183 is the application's port itself.
Request.Url.Scheme + "://" + Request.Url.Authority +
Request.ApplicationPath.TrimEnd('/') + "/";here
then I tried this one and it just returned "localhost" instead of domain names.
HttpContext.Current.Request.Url.Host;
I'm so curious to know where I'm going wrong. please let me know if any further information is needed.
A web server does not actually know what domains it can serve. It knows what domain was in the request headers in the thread for that request, but you can set up any domain you like to point to any server's IP address and the server will accept it.
If you have set up bindings by domain (using Server Name Indication), you can get that information from the objects in Microsoft.Web.Administration ( for each site in ServerManager.Sites, for each Binding in site.bindings, if Binding.Protocol = http or https and Binding.Host !="" then write out Binding.Host) but that won't tell you about any domains that aren't in bindings.
If you use request object it will only give details about the website where your code is running. You need to use Directory Services to connect to IIS and do you work.
[From already answered question]- Here's an article explaining how this could be done using classes from the System.DirectoryServices namespace as well as the Microsoft.Web.Administration namespace which was introduced with IIS 7.
You can use below code to get the Domain name of current server:
System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName
or you can use below code to get current user's domain:
System.Security.Principal.WindowsIdentity.GetCurrent().User. (string value)
Then you can trim it.
Happy coding.

Categories

Resources