Logging IP Addresses

Logging IP Addresses

Old forum URL: forums.lhotka.net/forums/t/7658.aspx


Revolucent posted on Saturday, September 19, 2009

I want to log a user's IP address, but only if the data portal is remote. (The column in which it will be stored in the database is nullable).

Although it's rare, users can potentially have multiple network cards, etc. (I'm not an expert on network, so perhaps this isn't a problem.)

What I'd like to have is some way to get the caller's IP address ONLY when I'm executing on the server inside of a remote data portal.

How can I do this?

Vinodonly replied on Sunday, September 20, 2009

chk this to get ip address..

http://msdn.microsoft.com/en-us/library/system.net.ipaddress.aspx

http://www.codeproject.com/KB/cs/network.aspx

i think you will have to get the ip address on the client side itself and then set it up in Applicationcontext which u can access from the remoting portal..

rsbaker0 replied on Sunday, September 20, 2009

Here is an article on doing this in .NET 3.5 from a WCF service:

http://blogs.msdn.com/phenning/archive/2007/08/08/remoteendpointmessageproperty-in-wcf-net-3-5.aspx

You can determine if you are on the server versus the client by checking ApplicationContext.ExecutionLocation.

If you later decide that you also want to be able to do this if the code to also work in a single-tier deployment, then you need to use ApplicationContext.LogicalExecutionLocation, and you would use a different method for retrieving the local IP than you would for determining the client IP server side.

Incidently, another wrinkle to possible consider is when accessing your application via terminal services. This can look like a single-tier deployment but the client is actually remote. There are mechanisms for getting the actual remote client's IP also if you are interested.

Revolucent replied on Sunday, September 20, 2009

I appreciate both of your answers.

I'm pretty familiar with WCF and System.Net.IPAddress. I understand how to get the ip address on the client side, and I also understand how to get it inside WCF.

What I want to do is a bit different. When I wrote my original post, I was pretty tired, so I think I sounded like a deer-in-the-headlights newb.

Imagine the following scenario: I have a CSLA object called Foo. It has been transported to the server using Rocky's WCF data portal. We're inside DataPortal_Insert, executing on the server.

[Transactional(TransactionalTypes.TransactionScope)]
void DataPortal_Insert()
{
/*
Here's where I want to get the IP address of the client
that serialized this object and sent it up to the server
to be reconstituted and persisted.
*/
}

So, the problem I have is that, as far as I know, I don't have any access to the RemoteEndPointMessageProperty when executing inside any of the DataPortal_XYZ methods. I don't have any access to WCF at all.

It looks like I'm going to have to get the IPAddress on the client side and pass it up to the server, although I regard that as a sub-optimal solution.

Revolucent replied on Sunday, September 20, 2009

Actually, I may have a solution. Pseudocode follows.

[Transactional(TransactionalTypes.TransactionScope)]
void DataPortal_Insert()
{
if (ApplicationContext.ExecutionLocation == ApplicationContext.ExecutionLocations.Server)
{
if (OperationContext.Current != null)
{
/* Do something with WCF's OperationContext. Hopefully we'll be able to get the IP address of the message sender, but I haven't tried yet. */
}
}
}

rsbaker0 replied on Sunday, September 20, 2009

^^^^^

I found this snippet by searching but haven't tried it yet:

OperationContext context = OperationContext.Current;

MessageProperties messageProperties = context.IncomingMessageProperties;

RemoteEndpointMessageProperty endpointProperty =

messageProperties[RemoteEndpointMessageProperty.Name]

as RemoteEndpointMessageProperty;



return string.Format("Hello {0}! Your IP address is {1} and your port is {2}",

value, endpointProperty.Address, endpointProperty.Port);

Copyright (c) Marimer LLC