IPAddress of a login system

前端 未结 6 2515
花落未央
花落未央 2020-12-22 05:42

Using the bellow code .

protected string GetUserIP()
{
    string strUserIP = string.Empty;
    if (HttpContext.Current.Request.ServerVariables[\"HTTP_X_FORW         


        
相关标签:
6条回答
  • 2020-12-22 05:50

    Client IP can be read from request:

    context.Request.ServerVariables["REMOTE_HOST"] 
    

    Here is code for getting more than just client IP address:

    string browserInfo =
                 "RemoteUser=" + context.Request.ServerVariables["REMOTE_USER"] + ";\n"
                + "RemoteHost=" + context.Request.ServerVariables["REMOTE_HOST"] + ";\n"
                + "Type=" + context.Request.Browser.Type + ";\n"
                + "Name=" + context.Request.Browser.Browser + ";\n"
                + "Version=" + context.Request.Browser.Version + ";\n"
                + "MajorVersion=" + context.Request.Browser.MajorVersion + ";\n"
                + "MinorVersion=" + context.Request.Browser.MinorVersion + ";\n"
                + "Platform=" + context.Request.Browser.Platform + ";\n"
                + "SupportsCookies=" + context.Request.Browser.Cookies + ";\n"
                + "SupportsJavaScript=" + context.Request.Browser.EcmaScriptVersion.ToString() + ";\n"
                + "SupportsActiveXControls=" + context.Request.Browser.ActiveXControls + ";\n"
                + "SupportsJavaScriptVersion=" + context.Request.Browser["JavaScriptVersion"] + "\n";
    

    (or)

    string IPAddress = string.Empty;
    string SearchName = string.Empty;
    
    String strHostName = HttpContext.Current.Request.UserHostAddress.ToString();
    
    IPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
    

    ServerVariables

    0 讨论(0)
  • 2020-12-22 05:56

    You can find IP Address in a way given below. It works for me :) If you want only one IP then pick up first IP from the list.

    var Dnshost = Dns.GetHostEntry(Dns.GetHostName());
    
    string ipAddress = "";
    
    IPAddress[] ipAddress = Dnshost.AddressList;
    ipAddress = ipAddress[0].ToString();
    

    Here "ipAddress[0]" will give you current IP of system. And if you want all IP's to get then iterate the AddressList as shown below:

    foreach (var ip in Dnshost.AddressList)
     {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    ipAddress = ip.ToString();
                }
     }
    

    NOTE: It will give you the IPv4 Addresses in case of "AddressFamily.InterNetwork" and if you need IPv6 Addresses you will use "AddressFamily.InterNetworkV6".

    Hope it will be helpful for you.

    0 讨论(0)
  • 2020-12-22 06:03

    It is of localhost ::1 if you use on web server you will get the correct one.

    Though it will depend on the configuration of the network from where the user is accessing your application.

    There can be firewall which doesn't expose the actual IP of the client system.

    0 讨论(0)
  • 2020-12-22 06:03

    To get the IpAddress use the below code

    string IPAddress = GetIPAddress();
        public string GetIPAddress()
        {
    
            IPHostEntry Host = default(IPHostEntry);
            string Hostname = null;
            Hostname = System.Environment.MachineName;
            Host = Dns.GetHostEntry(Hostname);
            foreach (IPAddress IP in Host.AddressList) {
                if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) {
                    IPAddress = Convert.ToString(IP);
                }
            }
            return IPAddress;
    
        }
    

    Source

    0 讨论(0)
  • 2020-12-22 06:08

    ::1 is the IPv6 localhost address, you can find more information here

    0 讨论(0)
  • 2020-12-22 06:15

    Method that gets IP address: ASP.NET, C#

    using System;
    using System.Web;
    
    namespace WebApplication1
    {
        public class Global : HttpApplication
        {
          protected void Application_BeginRequest(object sender, EventArgs e)
          {
            // Get request.
            HttpRequest request = base.Request;
    
            // Get UserHostAddress property.
            string address = request.UserHostAddress;
    
            // Write to response.
            base.Response.Write(address);
    
            // Done.
            base.CompleteRequest();
          }
        }
    }
    
    0 讨论(0)
提交回复
热议问题