Guruprasad J Rao
Guruprasad J Rao

Reputation: 29683

Get detail of who is invoking webservice in C#

I have a simple webservice which returns a string of message as below:

[WebMethod]
public string GetData()
{
     return "Here is your response";
}

So just to log details, is it possible to identify from which system the service has been invoked?

Upvotes: 0

Views: 64

Answers (2)

BWA
BWA

Reputation: 5764

You can use HttpContext by:

string ip = System.Web.HttpContext.Current.Request.UserHostAddress;

And there is more usefull informations.

Upvotes: 1

Jimmy
Jimmy

Reputation: 113

You can get the host address from the OperationContext.Current. The below code would do it.

RemoteEndpointMessageProperty endpoint = OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string address = endpoint.Address;

This will work for both self hosted and IIS hosted services. The Address might give a IPV6 address when the network is configured with it. So you may need to resolve using Dns.GetHostEntry(address) to get the IPV4 address.

Upvotes: 0

Related Questions