Récupérer IP client dans wcf
Bonjour,
Je suis confronter à un problème : compter le nombre de client connecté à mon web service.
J'ai pensé à comptabilisé le nombre de connexion en différenciant le nombre d'ip différente connecté à mon web service.
J'ai essayé de récupéré en vain l'adresse IP du client à sa connexion avec toutes les différentes méthodes que j'ai pu trouver sur internet :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
private string GetCustomerIP()
{
string CustomerIP = "";
try
{
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
}
catch
{ }
try
{
CustomerIP = System.Web.HttpContext.Current.Request.UserHostAddress;
}
catch
{ }
try
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
catch
{ }
try
{
string clientIP;
string ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ip))
{
string[] ipRange = ip.Trim().Split(',');
int le = ipRange.Length - 1;
clientIP = ipRange[le];
}
else
clientIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
catch
{ }
// Get message properties from current OperationContext
System.ServiceModel.Channels.MessageProperties props = System.ServiceModel.OperationContext.Current.IncomingMessageProperties;
// Find the RemoteEndpointMessageProperty
System.ServiceModel.Channels.RemoteEndpointMessageProperty prop = (System.ServiceModel.Channels.RemoteEndpointMessageProperty)props[System.ServiceModel.Channels.RemoteEndpointMessageProperty.Name];
// Retrieve the IP address
string addr = prop.Address;
int iPort = prop.Port;
return addr;
} |
Seulement toutes ces fonctions me retourne l'erreur suivante :
Citation:
La valeur n'est pas comprise dans la plage attendue.
Et l'utilisation du OperationContext me retourne une chaine d'adresse vide ("") !
J'utilise un web service avec une liaison WSDualHttpBinding et les caractéristique suivante :
Code:
1 2 3
|
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] |
Avez vous une idée d'où le problème peut venir ? et/ou Avez vous d'autres propositions pour connaitre le nombre de client actuellement connecté ?