Previously in other version of asp.net, I used these properties of HttpRequest
:
Request.ServerVariables["REMOTE_ADDR"]
Request.UserHostAddress
How can I achieve the same in ASP.NET Core?
Determining IP Address using $_SERVER Variable Method : There is another way to get the IP Address by using the $_SERVER['REMOTE_ADDR'] or $_SERVER['REMOTE_HOST'] variables. The variable in the $_SERVER array is created by the web server such as apache and those can be used in PHP.
Client IP addresses describe only the computer being used, not the user. If multiple users share the same computer, they will be indistinguishable. Many Internet service providers dynamically assign IP addresses to users when they log in.
First, click on your Start Menu and type cmd in the search box and press enter. A black and white window will open where you will type ipconfig /all and press enter. There is a space between the command ipconfig and the switch of /all. Your ip address will be the IPv4 address.
You can use IHttpContextAccessor
:
private IHttpContextAccessor _accessor;
public Foo(IHttpContextAccessor accessor)
{
_accessor = accessor;
}
Now you get IP address this way"
var ip = _accessor.HttpContext.Connection.RemoteIpAddress.ToString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With