How to get client machine name during login. C# MVC - c#

I tried this link in stackoverflow but not getting the client's name.
clientHostName = clientIpAddress = string.Empty;
try
{
clientIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(clientIpAddress))
{
clientIpAddress = Request.ServerVariables["REMOTE_ADDR"];
}
System.Net.IPAddress myIP = System.Net.IPAddress.Parse(clientIpAddress);
System.Net.IPHostEntry GetIPHost = System.Net.Dns.GetHostEntry(myIP);
clientHostName = GetIPHost.HostName; // Working in dev environment. Moving to QA env this is returning null
}
catch { }

Take a look at blowdarts answer in the post you linked. It is not possible to get the machine name of remote machines this way. If you are in control of the client application you may try to make the client explicitly send it's machine name instead.

Related

How to get client ipaddress and device name who logs in to my website using c#

I have searched and read almost every article related to my search about how to get client ipaddress and machine name but all the codes return only server ip and name. I've tried these codes also given below, it gives my PC ip on localhost but on the server, it gives server ip and name :
string stringHostName = Dns.GetHostName();
IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName);
and also used this method :
public string GetUserIPAddress()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDER_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
return addresses[0];
}enter code here
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}
It gives ::1 on localhost but on the server it gives server ip only.
Kindly help me if there any way possible to get client ipaddress and machine name using C#.

Why is my Azure public IP not showing up in Dns.GetHostAddresses?

I'm trying to set up a custom server on an Azure VM. I've assigned it a public IP address, which I'm able to reach and get into the server via Remote Desktop, so that part's working just fine.
But when I try to bind to the public IP address using the websocket-sharp library, it fails, saying "the host part isn't a local host name."
I've tracked this down to this file, where the following code block executes, and ends up returning false:
var host = System.Net.Dns.GetHostName ();
var addrs = System.Net.Dns.GetHostAddresses (host);
foreach (var addr in addrs) {
if (address.Equals (addr))
return true;
}
return false;
With a bit of debugging, I've determined that Dns.GetHostAddresses is showing internal IPs only, but not the external IP address. I've configured the IP address in Azure and attached it to the server, and I've turned on IP forwarding in the networking configuration and rebooted the VM, but the server still doesn't recognize its own external IP.
What am I missing?
What am I missing?
You could test Dns.GetHostAddresses with the local machine hostname, it also just could get the internal ip, it is not related to Azure VM.
If we want to get the public Ip of Azure VM with host name, we could use the Azure SDK to do that.
I also do a sample demo to get the public IP. Before that we need to registry Azure AD and assign corrosponding role to registried App. About how to registry Azure AD and create creditial file, you could refer to another SO thread and this link.
var subscriptiondId = "subscription Id";
var credentials = SdkContext.AzureCredentialsFactory.FromFile(#"path of creditial file");
var resouceGroup = "resouce group";
var hostName = "host name";
NetworkManagementClient networkManagement = new NetworkManagementClient(credentials) { SubscriptionId = subscriptiondId };
ComputeManagementClient computeManagement =
new ComputeManagementClient(credentials) {SubscriptionId = subscriptiondId};
var nic = computeManagement.VirtualMachines.GetAsync(resouceGroup, hostName).Result.NetworkProfile.NetworkInterfaces
.FirstOrDefault();
var networkIntefaceName = nic?.Id.Split('/').Last();
var ipConfiguration = networkManagement.NetworkInterfaces.GetAsync(resouceGroup, networkIntefaceName).Result.IpConfigurations.FirstOrDefault();
var publicIpAddressId = ipConfiguration?.PublicIPAddress.Id;
var ip = networkManagement.PublicIPAddresses.GetAsync(resouceGroup, publicIpAddressId?.Split('/').Last()

Bypass Proxy Server, ASP Website on IIS

I've actually deployed my asp website on IIS and everything works well. I have done some reading on how to bypass the proxy server but i'm still unsure on how to do it. The reason why i want to bypass the proxy server on the network is to be able to read client's ip address (the ip address behind the proxy). Frankly speaking, i don't have much knowledge on this topic..
I read from MSDN that you can add in lines of codes into Web.Config file to bypass the proxy server. But i'm not sure what to type in or how to use this defaultproxy tag.
Link
Right now i'm only retrieving either 127.0.0.1 which is localhost or the machine's network external ip address which is not what i want..
Anyone can point me in the right direction to getting ip address behind the proxy? Appreciate any help please. Thank you..
Codes i have been experimenting to get client's IP address:
protected void getIP_Click(object sender, EventArgs e)
{
String hostName = System.Net.Dns.GetHostName();
String clientIpAddressHostName = System.Net.Dns.GetHostAddresses(hostName).GetValue(0).ToString();
IP1.Text = clientIpAddressHostName;
String clientIpAddress = HttpContext.Current.Request.ServerVariables["REMOTE_HOST"].ToString();
IP2.Text = clientIpAddress;
String ip = null;
if (String.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
IP3.Text = ip;
String ipNext = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
IP4.Text = ipNext;
//String ipNextNext = HttpContext.Current.Request.UserHostAddress;
String ipNextNext = HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"].ToString();
IP5.Text = ipNextNext;
String last = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.GetValue(0).ToString();
IP6.Text = last;
Label2.Text = getIPAdd();
try
{
String externalIP;
externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
externalIP = (new Regex(#"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")).Matches(externalIP)[0].ToString();
Label1.Text = externalIP;
}
catch (Exception ex)
{
logManager log = new logManager();
log.addLog("IP Add", "IP Add", ex);
}
}
private String getIPAdd()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
return addresses[0];
}
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}
Results,
Requested Details:
I just used this to get the client IP address:
HttpContext.Current.Request.UserHostAddress
As I can see you have that one in your code too, but it's commented out.
It worked for me:

How to get user information when they access my website

I have a simple website in ASP.NET where I have loaded a DLL. I have published the site via IIS and I only want to show on the user side his Machine Name, logged in user and IP. I have tried the following:
My DLL:
namespace ClassLibrary1
{
public class Class1
{
public string getInfo()
{
IPAddress[] ips;
ips = Dns.GetHostAddresses(Dns.GetHostName());
string returns = null;
returns = Environment.MachineName + Convert.ToChar(9) + Environment.UserName;
foreach (IPAddress ip in ips)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
returns += Convert.ToChar(9) + ip.ToString();
}
return returns;
}
}
}
And in the website:
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
ClassLibrary1.Class1 cl = new ClassLibrary1.Class1();
Label2.Text = cl.getInfo();
}
}
The output is not what I expected. In my machine, when I access the site i get
MyMachineName Classic .NET AppPool MyIp
And when anyone else opens it, they also get those informations, not their machinename, logged in user and IP.
So my question is how to retrieve their info?
Thanks in advance.
You are pulling the stats from the machine that is serving the website, not the visitor's machine. You should probably take a look at the HttpRequest.ServerVariables NameValueCollection instead.
Some of those "variables", particularly the ones you are interested in, are derived from the headers in each web request from the client. Keep in mind that you aren't actually talking to the client's machine, these are sent to you from the client. Consequently, there's no guarantee that they will be accurate (proxy, etc.), if they're even there at all.
That said, the ones you are probably interested in are:
var ip = HttpRequest.ServerVariables["REMOTE_ADDR"];
var user = HttpRequest.ServerVariables["REMOTE_USER"]; // Windows auth
var user = HttpRequest.ServerVariables["LOGON_USER"]; // Non-Windows auth
var machine = HttpRequest.ServerVariables["REMOTE_HOST"];
Here's the list of variables to pick from.

How to get the server IP Address (in C# / asp.net)?

Is there a 1 line method to get the IP Address of the server?
Thanks
Request.ServerVariables["LOCAL_ADDR"];
From the docs:
Returns the server address on which the request came in. This is important on computers where there can be multiple IP addresses bound to the computer, and you want to find out which address the request used.
This is distinct from the Remote addresses which relate to the client machine.
From searching the net I found following code: (I couldn't find a single line method there)
string myHost = System.Net.Dns.GetHostName();
// Show the hostname
MessageBox.Show(myHost);
// Get the IP from the host name
string myIP = System.Net.Dns.GetHostEntry(myHost).AddressList[index].ToString();
// Show the IP
MessageBox.Show(myIP);
-> where index is the index of your ip address host (ie. network connection).
Code from: http://www.geekpedia.com/tutorial149_Get-the-IP-address-in-a-Windows-application.html
As other(s) have posted, System.Net.Dns.GetHostEntry is the way to go. When you access the AddressList property, you'll want to take the AddressFamily property into account, as it could return both IPv4 AND IPv6 results.
This method will return your machine public IP address when run this code on your PC and when you deploy your application on server will return Server IP address.
public static string Getpublicip()
{
try
{
string externalIP = "";
var request = (HttpWebRequest)WebRequest.Create("http://icanhazip.com.ipaddress.com/");
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
externalIP = new WebClient().DownloadString("http://icanhazip.com");
return externalIP;
}
catch (Exception e)
{
return "null";
}
}

Categories