How to get IP-Adess of the client (Windows autentication)

L

lvpaul

Hallo !

I am using IIS-Windows-Authentication in my intranet
(web.config <authentication mode="Windows" /> <identity
impersonate="true" />

How can I get the users (client) IP-Address ?

I think the username can be read with user.identity.name.

Thanks
aaapaul
 
M

Mark Rae

I am using IIS-Windows-Authentication in my intranet
(web.config <authentication mode="Windows" /> <identity
impersonate="true" />

How can I get the users (client) IP-Address ?

HttpContext.Current.Request.UserHostAddress

However, as has been mentioned countless times before, this is not 100%
reliable.
 
L

lvpaul

Hi Mark !

Your statement gives back 127.0.0.1.

This isn´t the IP of the client.

Any suggestions.
 
M

Mark Rae

1) Create a new WebForm called debug.aspx

2) Drop the code at the end of this post into it, and deploy it on your web
server

3) Browse to it from a client machine



<%@ Page language="c#" %>
<%@ Import Namespace="System" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>Debug for ASP.NET</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema
content="http://schemas.microsoft.com/intellisense/ie5">
<meta HTTP-EQUIV="Pragma" CONTENT="no-cache">

<script>

function toggleTable(pstrTable)
{
if(document.getElementById("tbl" + pstrTable).style.display =="none")
{
document.getElementById("tbl" + pstrTable).style.display="block";
document.getElementById("cmd" + pstrTable).value="Hide";
}
else
{
document.getElementById("tbl" + pstrTable).style.display="none";
document.getElementById("cmd" + pstrTable).value="Show";
}
}

</script>
</head>

<body><form>

<input type=button id=cmdCurrentIISContext value="Show"
onclick=toggleTable("CurrentIISContext"); style=width:32pt>
<b>Current server context running IIS (Environment)</b>
<table id="tblCurrentIISContext" border=1 style=display:none>
<tr>
<td><b>Key</b></td>
<td><b>Value</b></td>
</tr>
<!--
<tr>
<td>System.Security.Principal.WindowsIdentity.GetCurrent().Name</td>
<td><%// = System.Security.Principal.WindowsIdentity.GetCurrent().Name
%></td>
</tr>
-->
<tr>
<td>System.Environment.CurrentDirectory</td>
<td><% = System.Environment.CurrentDirectory.ToString() %></td>
</tr>
<tr>
<td>System.Environment.MachineName</td>
<td><% = System.Environment.MachineName.ToString() %></td>
</tr>
<tr>
<td>System.Environment.OSVersion</td>
<td>
<%
/*
4.00.1381 Windows NT 4.0
4.00.1381 Windows NT 4.0 Service Pack 6a 1999-11-30
5.00.2195 Windows 2000 (Windows NT 5.0)
5.00.2195 Windows 2000 (Windows NT 5.0) Service Pack 4 2003-06-26
5.1.2600 Windows XP (Windows NT 5.1)
5.1.2600.2096 Windows XP (Windows NT 5.1) Service Pack 2 RC1 2004-03-11
5.1.2600.2149 Windows XP (Windows NT 5.1) Service Pack 2 RC2 2004-06-10
5.2.3663 Windows Server 2003 RC1
5.2.3718 Windows Server 2003 RC2
5.2.3790 Windows Server 2003 RTM (Windows NT 5.2) 2003-04
6.0.4051 Windows "Longhorn" Client Preview 1 2003-11-02
6.0.4074 Windows "Longhorn" Client Preview 2 2004-05-04
*/
Response.Write(System.Environment.OSVersion.ToString());
System.OperatingSystem osInfo = System.Environment.OSVersion;

switch(osInfo.Platform) // determine the platform
{
case System.PlatformID.Win32Windows: // Win95, Win98, Win98SE, WinMe
{
switch (osInfo.Version.Minor)
{
case 0:
{
Response.Write(" (Windows 95)");
break;
}
case 10:
{
if(osInfo.Version.Revision.ToString() == "2222A")
{
Response.Write(" (Windows 98 Second Edition)");
}
else
{
Response.Write(" (Windows 98)");
}
break;
}
case 90:
{
Response.Write(" (Windows Me)");
break;
}
}
break;
}
case System.PlatformID.Win32NT: // WinNT Win2k, WinXP, Win2k3
{
switch(osInfo.Version.Major)
{
case 3:
{
Response.Write(" (Windows NT 3.51)");
break;
}
case 4:
{
Response.Write(" (Windows NT 4.0)");
break;
}
case 5:
{
switch(osInfo.Version.Minor)
{
case 0:
{
Response.Write(" (Windows 2000)");
break;
}
case 1:
{
Response.Write(" (Windows XP)");
break;
}
case 2:
{
switch(osInfo.Version.Build)
{
case 3663:
{
Response.Write(" (Windows Server 2003 RC1)");
break;
}
case 3718:
{
Response.Write(" (Windows Server 2003 RC2)");
break;
}
case 3790:
{
Response.Write(" (Windows Server 2003)");
break;
}
}
break;
}
}
break;
}
case 6:
{
switch(osInfo.Version.Build)
{
case 4051:
{
Response.Write(" (Windows \"Longhorn\" Client Preview 1)");
break;
}
case 4074:
{
Response.Write(" (Windows \"Longhorn\" Client Preview 2)");
break;
}
}
break;
}
default :
{
Response.Write(" (Unknown)");
break;
}
}
break;
}
}
%>
</td>
</tr>
<tr>
<td>System.Environment.SystemDirectory</td>
<td><% = System.Environment.SystemDirectory.ToString() %></td>
</tr>
<tr>
<td>System.Environment.UserDomainName</td>
<td><% = System.Environment.UserDomainName.ToString() %></td>
</tr>
<tr>
<td>System.Environment.UserName</td>
<td><% = System.Environment.UserName.ToString() %></td>
</tr>
<tr>
<td>System.Environment.Version</td>
<td>
<%
Response.Write(System.Environment.Version.ToString());
switch(System.Environment.Version.ToString())
{
case "1.0.3705.000" :
{
Response.Write(" (.NET 1.0)");
break;
}
case "1.0.3705.209" :
{
Response.Write(" (.NET 1.0 SP1)");
break;
}
case "1.0.3705.288" :
{
Response.Write(" (.NET 1.0 SP2)");
break;
}
case "1.0.3705.6018" :
{
Response.Write(" (.NET 1.0 SP3)");
break;
}
case "1.1.4322.573" :
{
Response.Write(" (.NET 1.1)");
break;
}
case "1.1.4322.2032" :
{
Response.Write(" (.NET 1.1 SP1)");
break;
}
}
%>
</td>
</tr>
<tr>
<td>System.Environment.WorkingSet</td>
<td><% = (System.Environment.WorkingSet / (1000 *
1024)).ToString("#,###0") + "Mb (" +
System.Environment.WorkingSet.ToString("#,###0") + ")" %></td>
</tr>
</table>
<hr>
<input type=button id=cmdEnvironment value="Show"
onclick=toggleTable("Environment"); style=width:32pt>
<b>Environment Variables (Environment.GetEnvironmentVariables())</b><br>
<table id="tblEnvironment" border=1 style=display:none>
<tr>
<td><b>Key</b></td>
<td><b>Value</b></td>
</tr>
<%foreach (DictionaryEntry de in Environment.GetEnvironmentVariables()) {%>
<tr>
<td>
<%= de.Key %>
</td>
<td>
<%= de.Value %>
</td>
</tr>
<% } %>
</table>
<hr>
<input type=button id=cmdRequest value="Show"
onclick=toggleTable("Request"); style=width:32pt>
<b>Request Object (HttpContext.Current.Request)</b><br>
<table id="tblRequest" border=1 style=display:none>
<tr>
<td><b>Key</b></td>
<td><b>Value</b></td>
</tr>
<tr>
<td>
.UserHostAddress
</td>
<td>
<%= Request.UserHostAddress %>
</td>
</tr>
<tr>
<td>
.UserHostName
</td>
<td>
<%= Request.UserHostName %>
</td>
</tr>
</table>
<hr>
<input type=button id=cmdRequestServerVariables value="Show"
onclick=toggleTable("RequestServerVariables"); style=width:32pt>
<b>Server Variables (HttpContext.Current.Request.ServerVariables)</b><br>
<table id="tblRequestServerVariables" border=1 style=display:none>
<tr>
<td><b>Key</b></td>
<td><b>Value</b></td>
</tr>
<%foreach (string name in HttpContext.Current.Request.ServerVariables) {%>
<tr>
<td>
<%= name %>
</td>
<td>
<%
if (Request.ServerVariables[name] != "")
{
Response.Write(Request.ServerVariables[name]);
}
else
{
Response.Write("&nbsp;");
}
%>
</td>
</tr>
<% } %>
</table>
<hr>
<input type=button id=cmdApplication value="Show"
onclick=toggleTable("Application"); style=width:32pt>
<b>Application Variables (HttpContext.Current.Application)</b><br>
<table id="tblApplication" border=1 style=display:none>
<tr>
<td><b>Key</b></td>
<td><b>Value</b></td>
</tr>
<%foreach (string name in HttpContext.Current.Application) {%>
<tr>
<td>
<%= name %>
</td>
<td>
<%= Application[name] %>
</td>
</tr>
<% } %>
</table>
<hr>
<input type=button id=cmdSession value="Show"
onclick=toggleTable("Session"); style=width:32pt>
<b>Session Variables (HttpContext.Current.Session)</b><br>
<table id="tblSession" border=1 style=display:none>
<tr>
<td><b>Key</b></td>
<td><b>Value</b></td>
</tr>
<%foreach (string name in HttpContext.Current.Session) {%>
<tr>
<td>
<%= name %>
</td>
<td>
<%= Session[name] %>
</td>
</tr>
<% } %>
</table>
<hr>
<input type=button id=cmdRequestCookies value="Show"
onclick=toggleTable("RequestCookies"); style=width:32pt>
<b>Cookies (HttpContext.Current.Request.Cookies)</b><br>
<table id="tblRequestCookies" border=1 style=display:none>
<tr>
<td><b>Key</b></td>
<td><b>Value</b></td>
</tr>
<%foreach (string name in HttpContext.Current.Request.Cookies) {%>
<tr>
<td>
<%= name %>
</td>
<td>
<%= Request.Cookies[name] %>
</td>
</tr>
<% } %>
</table>
<hr>
<%
/*
'System.Web.HttpBrowserCapabilities' does not contain a definition for
'GetEnumerator'
*/
%>
<input type=button id=cmdRequestBrowser value="Show"
onclick=toggleTable("RequestBrowser"); style=width:32pt>
<b>Browser (HttpContext.Current.Request.Browser)</b><br>
<table id="tblRequestBrowser" border=1 style=display:none>
<tr>
<td><b>Key</b></td>
<td><b>Value</b></td>
</tr>
<tr>
<td>.ActiveXControls</td>
<td><%= HttpContext.Current.Request.Browser.ActiveXControls %></td>
</tr>
<tr>
<td>.AOL</td>
<td><%= HttpContext.Current.Request.Browser.AOL %></td>
</tr>
<tr>
<td>.BackgroundSounds</td>
<td><%= HttpContext.Current.Request.Browser.BackgroundSounds %></td>
</tr>
<tr>
<td>.Beta</td>
<td><%= HttpContext.Current.Request.Browser.Beta %></td>
</tr>
<tr>
<td>.Browser</td>
<td><%= HttpContext.Current.Request.Browser.Browser %></td>
</tr>
<tr>
<td>.CDF</td>
<td><%= HttpContext.Current.Request.Browser.CDF %></td>
</tr>
<tr>
<td>.ClrVersion</td>
<td><%= HttpContext.Current.Request.Browser.ClrVersion %></td>
</tr>
<tr>
<td>.Cookies</td>
<td><%= HttpContext.Current.Request.Browser.Cookies %></td>
</tr>
<tr>
<td>.Crawler</td>
<td><%= HttpContext.Current.Request.Browser.Crawler %></td>
</tr>
<tr>
<td>.EcmaScriptVersion</td>
<td><%= HttpContext.Current.Request.Browser.EcmaScriptVersion %></td>
</tr>
<tr>
<td>.Frames</td>
<td><%= HttpContext.Current.Request.Browser.Frames %></td>
</tr>
<tr>
<td>.JavaApplets</td>
<td><%= HttpContext.Current.Request.Browser.JavaApplets %></td>
</tr>
<tr>
<td>.JavaScript</td>
<td><%= HttpContext.Current.Request.Browser.JavaScript %></td>
</tr>
<tr>
<td>.MajorVersion</td>
<td><%= HttpContext.Current.Request.Browser.MajorVersion %></td>
</tr>
<tr>
<td>.MinorVersion</td>
<td><%= HttpContext.Current.Request.Browser.MinorVersion %></td>
</tr>
<tr>
<td>.MSDomVersion</td>
<td><%= HttpContext.Current.Request.Browser.MSDomVersion %></td>
</tr>
<tr>
<td>.Platform</td>
<td><%= HttpContext.Current.Request.Browser.Platform %></td>
</tr>
<tr>
<td>.Tables</td>
<td><%= HttpContext.Current.Request.Browser.Tables %></td>
</tr>
<tr>
<td>.Type</td>
<td><%= HttpContext.Current.Request.Browser.Type %></td>
</tr>
<tr>
<td>.VBScript</td>
<td><%= HttpContext.Current.Request.Browser.VBScript %></td>
</tr>
<tr>
<td>.Version</td>
<td><%= HttpContext.Current.Request.Browser.Version %></td>
</tr>
<tr>
<td>.W3CDomVersion</td>
<td><%= HttpContext.Current.Request.Browser.W3CDomVersion %></td>
</tr>
<tr>
<td>.Win16</td>
<td><%= HttpContext.Current.Request.Browser.Win16 %></td>
</tr>
<tr>
<td>.Win32</td>
<td><%= HttpContext.Current.Request.Browser.Win32 %></td>
</tr>
</table>
<hr>
<input type=button id=cmdNavigator value="Show"
onclick=toggleTable("Navigator"); style=width:32pt>
<b>Navigator Properties (javascript:navigator)</b><br>
<table id="tblNavigator" border=1 style=display:none>
<tr>
<td><b>Property</b></td>
<td><b>Value</b></td>
</tr>
<tr>
<td>appCodeName</td>
<td><script>document.write(navigator.appCodeName);</script></td>
</tr>
<tr>
<td>appMinorVersion</td> <!--appMajorVersion doesn't exist, for some
reason!-->
<td><script>document.write(navigator.appMinorVersion);</script></td>
</tr>
<tr>
<td>appName</td>
<td><script>document.write(navigator.appName);</script></td>
</tr>
<tr>
<td>appVersion</td>
<td><script>document.write(navigator.appVersion);</script></td>
</tr>
<tr>
<td>browserLanguage</td>
<td><script>document.write(navigator.browserLanguage);</script></td>
</tr>
<tr>
<td>constructor</td>
<td><script>document.write(navigator.constructor);</script></td>
</tr>
<tr>
<td>cookieEnabled</td>
<td><script>document.write(navigator.cookieEnabled);</script></td>
</tr>
<tr>
<td>cpuClass</td>
<td><script>document.write(navigator.cpuClass);</script></td>
</tr>
<tr>
<td>javaEnabled()</td>
<td><script>document.write(navigator.javaEnabled());</script></td>
</tr>
<tr>
<td>language</td>
<td><script>document.write(navigator.language);</script></td>
</tr>
<tr>
<td>onLine</td>
<td><script>document.write(navigator.onLine);</script></td>
</tr>
<tr>
<td>opsProfile</td>
<td><script>document.write(navigator.opsProfile);</script></td>
</tr>
<tr>
<td>platform</td>
<td><script>document.write(navigator.platform);</script></td>
</tr>
<tr>
<td>securityPolicy</td>
<td><script>document.write(navigator.securityPolicy);</script></td>
</tr>
<tr>
<td>systemLanguage</td>
<td><script>document.write(navigator.systemLanguage);</script></td>
</tr>
<tr>
<td>taintEnabled()</td>
<td><script>document.write(navigator.taintEnabled());</script></td>
</tr>
<tr>
<td>userAgent</td>
<td><script>document.write(navigator.userAgent);</script></td>
</tr>
<tr>
<td>userLanguage</td>
<td><script>document.write(navigator.userLanguage);</script></td>
</tr>
<tr>
<td>userProfile</td>
<td><script>document.write(navigator.userProfile);</script></td>
</tr>
</table>
<hr>
<input type=button id=cmdScreen value="Show" onclick=toggleTable("Screen");
style=width:32pt>
<b>Screen Properties (javascript:screen)</b><br>
<table id="tblScreen" border=1 style=display:none>
<tr>
<td><b>Property</b></td>
<td><b>Value</b></td>
</tr>
<tr>
<td>availHeight</td>
<td><script>document.write(screen.availHeight);</script></td>
</tr>
<tr>
<td>availLeft</td>
<td><script>document.write(screen.availLeft);</script></td>
</tr>
<tr>
<td>availTop</td>
<td><script>document.write(screen.availTop);</script></td>
</tr>
<tr>
<td>availWidth</td>
<td><script>document.write(screen.availWidth);</script></td>
</tr>
<tr>
<td>colorDepth</td>
<td><script>document.write(screen.colorDepth);</script></td>
</tr>
<tr>
<td>height</td>
<td><script>document.write(screen.height);</script></td>
</tr>
<tr>
<td>pixelDepth</td>
<td><script>document.write(screen.pixelDepth);</script></td>
</tr>
<tr>
<td>width</td>
<td><script>document.write(screen.width);</script></td>
</tr>
</table>

</form></body>

</html>
 
L

lvpaul

Thanks Mark !

Your script runs well. I can see many variables and value.

But what I need is the Windows-IP of the client. It isn´t shown.

Is this possible at all to get this value?
 
L

lvpaul

I need something like this:

Dim hostaddresses() As IPAddress
hostaddresses = Dns.GetHostByName(Dns.GetHostName).AddressList
Dim i As Integer
For i = 0 To hostaddresses.GetLength(0) - 1
Response.Write(hostaddresses(i).ToString & "<br>")
Next
 
M

Mark Rae

But what I need is the Windows-IP of the client. It isn´t shown.

You've got pretty much all of the possibilities in that script.

When I run it locally, several of the variables contain my machine's
internal IP address i.e. 192.168.0.4

When I browse to it on my public website, they correctly show my cable
modem's external IP address.

I can only think that there's something on your network which is
interfering...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top