How to intercept a message before the token authentication phase in a wcf service

50 Views Asked by At

I'm new to WCF. I have a client that establishes a connection to a WCF service by sending a username and password as a token. The service has a custom token authenticator which extends the UserNameSecurityTokenAuthenticator class to implement custom validation logic.

I want to figure out what the client IP address is from the server side and have it available in my custom token authenticator class

I've tried to add message inspectors and endpoint inspectors but these points are never hit. I have a customServiceCredentials class that extends the ServiceCredentials class. Is this where I can intercept the message before authentication.

I know using OperationContext I can get the clientIp, however I just don't know where to intercept the message before it hits the token authenticator

1

There are 1 best solutions below

1
QI You On

I think the BeforeSendRequest method can get the clientip before you send the request. But after your testing, this method won't work.

This method can be tried. This method was discovered on a post that is a bit old but I thought you could give it a try.

OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint = 
prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;

This is the original post:Obtaining client IP address