C# .NET - Fetch host name - Asked By Anurag on 22-Mar-12 08:36 AM

Earn up to 10 extra points for answering this tough question.

I need to get hostname of system when a user log into my web application (intranet). I am using  "System.Net.Dns.GetHostEntry(Request.ServerVariables["remote_addr"]).HostName" to get hostname but receive following error:

Message: Exception of type 'System.Web.HttpUnhandledException' was thrown.

StackTrace: at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.account_login_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\qms\29941156\2d34edbf\App_Web_0gg44vc5.4.cs:line 0 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Content: System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Net.Sockets.SocketException (0x80004005): The requested name is valid, but no data of the requested type was found at System.Net.Dns.InternalGetHostByAddress(IPAddress address, Boolean includeIPv6) at System.Net.Dns.GetHostEntry(String hostNameOrAddress) at QMS.Account.Login.LoginUser_LoggedIn(Object sender, EventArgs e) in myWebSolutionPath\Account\Login.aspx.cs:line 145 at System.Web.UI.WebControls.Login.OnLoggedIn(EventArgs e) at System.Web.UI.WebControls.Login.AttemptLogin() at System.Web.UI.WebControls.Login.OnBubbleEvent(Object source, EventArgs e) at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) at System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.account_login_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\mysitefolder\29941156\2d34edbf\App_Web_0gg44vc5.4.cs:line 0 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Need you help. Thanks in advance.

D Company replied to Anurag on 22-Mar-12 09:45 AM
GetHostEntry Method needs to pass proper host name , then u ccan fetch the all relevant information in your case the passing value is not correct to this method, if u only want to know the host name (as u have mentioned) then do as follows

string hostNameofLoggedInUs = System.Net.Dns.GetHostName();

Let me know in case u need more help on this
Regards
D
       
[)ia6l0 iii replied to Anurag on 22-Mar-12 12:24 PM
The suggestion that my fellow member posted above returns the hostname of the webserver. But I guess, you are looking for your client's hostname and which is why you are referring to the "remote_addr" server variable. 

First of all, you have to enable Reverse DNS Lookup for you IIS.

And thanks for putting the entire stack trace. It is evident that you are using ASP.Net v4.0 as your framework. Dns GetHostEntry method behaves a little differently in 4.0. The same has been documented in the feedback posted at Microsoft connect , regarding this same issue. 

Below is the url.
http://connect.microsoft.com/VisualStudio/feedback/details/561083/dns-gethostentry-behaves-differently-in-net-4-0-than-previous-versions

However, also note that this works only on intranet. If you go on the internet, this would not be valid as DNS names would be returned instead of host names. 



kalpana aparnathi replied to Anurag on 22-Mar-12 02:23 PM
hi,

Try this way:

string getUserHostAddress = HttpContext.Current.Request.UserHostAddress;
string getUserHostName = HttpContext.Current.Request.UserHostName;
 
Request.ServerVariables("REMOTE_ADDR");
Request.ServerVariables("REMOTE_HOST");

Use link: http://www.w3schools.com/asp/coll_servervariables.asp

Hope it will useful !!!!!!!!!!!!!!!!!

Regards,
Anurag replied to [)ia6l0 iii on 27-Mar-12 12:19 AM
Thanks Diablo. Yes I was looking for clients host name only, you are spot on :)
If this works for nearly all the client systems except 1, so I suppose Reverse DNS lookup would be enabled. Any way thanks for sharing this info as I dont have much server level knowledge and as you mentioned it, I came to know what Forward and Reverse DNS lookup is, Thanks.

I found that ms link earlier but wasn't sure if it is the same issue as mine. 

Yes, you are right again, this application is intranet application.

I got this problem resolved temporarily by asking my it support team to reset that particular system's host name who was causing the exception. I am afraid that this might happen again as it has happened once.
[)ia6l0 iii replied to Anurag on 27-Mar-12 10:13 AM
Glad to help!

The MS link could be a solution as we move towards newer versions of framework, and forget that old code can fail/break with the newer versions. And as it is an intranet application, a lot of things depends on the way the intranet and the domain servers are set up. There could be lots of standards compromised which could have led to the messed up host name. Glad, you got a temporary fix.