分享

Silverlight获取客户端IP

 Jcstone 2014-05-05

SL在客户端因为安全方面的原因,做了很多限制,直接不能得到,但是可以通过ASPX页面里写代码获取IP地址,然后传给Silverlight。
参考如下:
1、在aspx.cs的page_load中添加
Silverlight1.InitParameters = "IpAddress=" + HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

2、在App.xaml.cs的Application_Startup中修改
string ip = e.InitParams["IpAddress"];
this.RootVisual = new MainPage(ip);

3、修改MainPage.xaml.cs的构造函数
public MainPage(string ipAddress) {
MessageBox.Show(ipAddress);
}


 
通过WCF从服务器端获取就可以了。

public class Service1 : IService1
{
     public string DoWork()
     {
          OperationContext operationContext = OperationContext.Current;
          MessageProperties messageProperties = operationContext.IncomingMessageProperties;
          RemoteEndpointMessageProperty remoteEndpointProperty = messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;

        return string.Format("Your IP address is {0} and your port is {1}",remoteEndpointProperty.Address, remoteEndpointProperty.Port);
      }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多