Post by Markus MayerHello again.
In the server side of the remoting application, is it possible to see
the IP or MAC address of the client's interface? We're trying to lock
some of our Broker's functions to the local network.
I found a piece of code that matched the 'find out client's ip address
on the server' criteria, but I never used it myself.
It basically works by setting the call context on every remoting call
using an injector. This call context can be read on the server side and
thus the ip address (or any other given data) can be transported from
the client(s) to the server.
Cheers,
Udo
Here's the code and it would be nice if you posted your solution here,
if applicable ;)
class ClientIPInjectorSink : BaseChannelObjectWithProperties,
IServerChannelSink
{
private IServerChannelSink nextSink;
public ClientIPInjectorSink(IServerChannelSink nextSink)
{
this.nextSink = nextSink;
}
#region IServerChannelSink Members
public Stream GetResponseStream(
IServerResponseChannelSinkStack sinkStack,
object state,
IMessage msg,
ITransportHeaders headers)
{
return null;
}
public ServerProcessing ProcessMessage(
IServerChannelSinkStack sinkStack, IMessage requestMsg,
ITransportHeaders requestHeaders, Stream requestStream,
out IMessage responseMsg,
out ITransportHeaders responseHeaders,
out Stream responseStream)
{
//get the client's ip address, and put it in the call context.
//This value will be extracted later so we can determine
//the actual address of the client.
try
{
IPAddress ipAddr =
(IPAddress)requestHeaders[CommonTransportKeys.IPAddress];
CallContext.SetData("ClientIP", ipAddr);
}
catch (Exception)
{
//do nothing
}
//pushing onto stack and forwarding the call
sinkStack.Push(this, null);
ServerProcessing srvProc = nextSink.ProcessMessage(
sinkStack, requestMsg, requestHeaders, requestStream,
out responseMsg, out responseHeaders, out responseStream);
if (srvProc == ServerProcessing.Complete)
{
//NOTE: implement post processing
}
return srvProc;
}
public void AsyncProcessResponse(
IServerResponseChannelSinkStack sinkStack,
object state, IMessage msg,
ITransportHeaders headers, Stream stream)
{
//get the client's ip address, and put it in the call context.
//This value will be extracted later so we can determine
//the actual address of the client.
try
{
IPAddress ipAddr =
(IPAddress)headers[CommonTransportKeys.IPAddress];
CallContext.SetData("ClientIP", ipAddr);
}
catch (Exception)
{
//do nothing
}
//forward to stack for further processing
sinkStack.AsyncProcessResponse(msg, headers, stream);
}
public IServerChannelSink NextChannelSink
{
get { return nextSink; }
}
#endregion
}
class ClientIPInjectorSinkProvider : IServerChannelSinkProvider
{
private IServerChannelSinkProvider nextProvider;
#region IServerChannelSinkProvider Members
public IServerChannelSink CreateSink(IChannelReceiver channel)
{
//create other sinks in the chain
IServerChannelSink nextSink = nextProvider.CreateSink(channel);
return new ClientIPInjectorSink(nextSink);
}
public IServerChannelSinkProvider Next
{
get { return nextProvider; }
set { nextProvider = value; }
}
public void GetChannelData(IChannelDataStore channelData)
{
//not needed
}
#endregion
}