Get the computer name
you can just use without function:
System.Net.Dns.GetHostName
or:
Environment.MachineName
How to get clients computer name using asp.net mvc?
No you can't, unless the client sends such info when making HTTP requests. By "client", I'm referring to any - some app, browser, etc.
You can inspect your own browser request flow using standard browser dev tools and see exactly what info your browser is sending. It will not have your machine name (unless something in your machine is and that would likely be a problem).
That said, HTTP Header data is what you have aside from standard network info such as IP address (which also isn't guaranteed to be the client's IP address - it could be the client's network address). The closest you can get to is hostname
if it exists, and even then, just like IP address, is not guaranteed to be machine name.
A possible exception would be in an internal network (LAN).
How do I get the local machine name in C#?
System.Environment.MachineName
It works unless a machine name has more than 15 characters.
How to get client's computer name
Two things to consider:
- Take into account that
HttpContext.Current.Request.ServerVariables.Item("REMOTE_HOST"
) will return the name of the host making the request, not the address. - Try doing
System.Net.Dns.GetHostByAddress(Request.ServerVariables.Item("REMOTE_HOST")).HostName
instead.
You can find a list of the Server Variables at Microsoft's MSDN website.
Hope that helps,
How to get the computer name using the IP Address
Here is how to obtain it on a different machine. This will also give you the IPv4 version.
class getIP
{
public getIP()
{
IPAddress ip = Dns.GetHostEntry("whybla01").AddressList.Where(o => o.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).First();
Console.WriteLine(ip);
string name = Dns.GetHostEntry(ip).HostName.ToString();
Console.WriteLine(name);
}
}
How to get full host name in C#?
You can use System.Net.Dns.GetHostEntry:
var fullName = System.Net.Dns.GetHostEntry(string.Empty).HostName;
If an empty string is passed as the
hostNameOrAddress
argument, then this method returns the IPv4 and IPv6 addresses of the local host.
Related Topics
Marshal C++ Struct Array into C#
How to Post an Array of Complex Objects with JSON, Jquery to ASP.NET MVC Controller
Pass Complex Parameters to [Theory]
Accessing Password Protected Network Drives in Windows in C#
Parallel.Foreach VS Task.Run and Task.Whenall
Programmatically Add Column & Rows to Wpf Datagrid
Format of the Initialization String Does Not Conform to Specification Starting at Index 0
Show Properties of a Navigation Property in Datagridview (Second Level Properties)
Right Click Context Menu for Datagridview
Getting the Current Tab's Url from Google Chrome Using C#
Is There a Downside to Adding an Anonymous Empty Delegate on Event Declaration
C# Pass by Value VS. Pass by Reference
Ef Including Other Entities (Generic Repository Pattern)
How to Hide Only the Close (X) Button