Read this article in your language IT | EN | DE | ES
Et voilà vous êtes repéré ! :-)
Mieux que Hadopi : tout aussi efficace mais beaucoup moins cher pour le contribuable :-)
Silverlight ne permet bien entendu du pas d’accéder à l’IP de l’hôte. Mais en rusant un peu, c’est à dire en utilisant un service WCF distant qui lui peut tout se permettre et retourner l’information à l’application Silverlight cette dernière peu savoir qui la manipule.
Il faut donc un service WCF avec une méthode retournant l’IP de l’appelant :
1: using System.ServiceModel;
2: using System.ServiceModel.Activation;
3: using System.Web;
4:
5: namespace MyIP.Web
6: {
7: [ServiceContract(Namespace = "")]
8: [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
9: public class ServiceIp
10: {
11: [OperationContract]
12: public string GetCustomerIP()
13: {
14: var customerIp = "";
15:
16: if( HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
17: customerIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
18: else if (HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] != null)
19: customerIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
20: return customerIp;
21: }
22: }
23: }
Si vous utilisez un Service WCF pour Silverlight, toute la configuration est automatique et votre application SL se retrouve avec une fichier “ServiceReferences.ClientConfig” dont le contenu est le suivant :
1: <configuration>
2: <system.serviceModel>
3: <bindings>
4: <customBinding>
5: <binding name="CustomBinding_ServiceIP">
6: <binaryMessageEncoding />
7: <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
8: </binding>
9: </customBinding>
10: </bindings>
11: <client>
12: <endpoint address="http://www.votresite.com/MyIP/ServiceIP.svc"
13: binding="customBinding" bindingConfiguration="CustomBinding_ServiceIP"
14: contract="ServiceReferenceIP.ServiceIP" name="CustomBinding_ServiceIP" />
15: </client>
16: </system.serviceModel>
17: </configuration>
L’application SL est complétée par un “Service References”pointant le service WCF.
Ne reste plus, côté application Silverlight à invoquer la méthode retournant l’adresse IP :
1: // MainPage()
2: var client = new ServiceReferenceIP.ServiceIPClient();
3: client.GetCustomerIPCompleted += client_GetCustomerIPCompleted;
4: client.GetCustomerIPAsync();
5: * * *
6: GetCustomerIPCompleted(object sender, ServiceReferenceIP.GetCustomerIPCompletedEventArgs e)
7: {
8: ourIP.Text = "Votre IP est : " + e.Result;
9: }
Rien de bien compliqué donc…
Le projet pour faire joujou chez vous :
Stay Tuned !