Obtain an IP address (reprint) client and server-side in ASP.NET Core in

With the development of ASP.NET, there are different ways to access client IP address from the request. WebForms and MVC Web application simply access the current HTTP request context.

var ip = HttpContext.Current.Request.UserHostAddress;

Or just direct reference to the current Request

var ip = Request.UserHostAddress;

 

However, this does not work in ASP.NET Core 2.0 and later. You must be injected from ConfigureServices methods Startup.cs class HttpContextAccessor instance.

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddMvc();
}

 

Now we need to use it and assigned to the controller level variables declared in our controller constructor so that it can be accessed from all Actions controller, note that we're still using the NetworkInterface.GetAllNetworkInterfaces () method to obtain the IP addresses of all network cards on the server:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using System.Net;
using System.Net.NetworkInformation;
using System.Linq;
using System.Net.Sockets;

namespace AspNetCoreIP.Controllers
{
    public class HomeController : Controller
    {
        protected readonly IHttpContextAccessor httpContextAccessor;

        public HomeController(IHttpContextAccessor httpContextAccessor)
        {
            this.httpContextAccessor = httpContextAccessor;
        }

        publicIndex IActionResult () 
        { 
            // Get the IP address of the client 
            String ClientIPAddress = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.MapToIPv4 () the ToString ();.
             The this .ViewData [ " ClientIPAddress " ] = ClientIPAddress; 

            // get all the cards on the server IP address 
            of NetworkInterface [] = Networks NetworkInterface.GetAllNetworkInterfaces ();
             String serverIpAddresses = String .Empty; 

            the foreach ( var Network in Networks) 
            { 
                var ipAddress = network.GetIPProperties().UnicastAddresses.Where(p => p.Address.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(p.Address)).FirstOrDefault()?.Address.ToString();

                serverIpAddresses += network.Name + ":" + ipAddress + "|";
            }

            this.ViewData["ServerIpAddresses"] = serverIpAddresses;

            return View();
        }
    }
}

Establish MVC view Index.cshtml, to display the IP address of the client and server side:

@ { 
    Layout = null; 
} 

<! DOCTYPE HTML > 

< HTML > 
< head > 
    < Meta name = "the viewport" Content = "width = Device-width"  /> 
    < title > Index </ title > 
</ head > 
< body > 
    < div > 
        client IP address: @ this.ViewData [ "ClientIPAddress"] ToString (). 
    </ div > 
    < div > 
        IP address of the server of all network adapters: @ this.ViewData [ "ServerIpAddresses"].ToString()
    </div>
</body>
</html>

 

Why sometimes httpContextAccessor.HttpContext.Connection.RemoteIpAddress to get the client IP address is empty?

How do I get client IP address in ASP.NET CORE?

 

Description link

 

Guess you like

Origin www.cnblogs.com/OpenCoder/p/11411414.html