简体   繁体   中英

How to make Silverlight client aware of user's IP address

I'm trying to figure out a way to get a Silverlight Client to be aware of the IP address of the current user. I've seen this information similarly before, but in reference to passing it back to the server, which is different from my purpose.

I'm trying to write a simple app that changes the source of the MediaElement depending on the IP address of the user. Hence, the IP address is only needed on the client side.

Is there a way to find out the IP address without using a webservice? If I must use a webservice, which one would be good to use?

After working on this project for a while, I think I found a simpler solution when hosted within an ASP.net page.

<body>
    <form id="form1" runat="server" style="height:100%">
    <div id="silverlightControlHost">
       <object id="SilverlightPlugin" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">

       <param name="source" value="ClientBin/VideoPlayer.xap"/>
       <param name="onload" value="onload" />
       <param name="initParams" value="txtUserIP=<%=Request.UserHostAddress %>,cc=true,m=/relative"/>
       <param name="onError" value="onSilverlightError" />
       <param name="background" value="white" />
       <param name="minRuntimeVersion" value="3.0.40624.0" />
       <param name="autoUpgrade" value="true" />
       <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none">
        <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
       </a>
        </object>    
    </div>
    </form>
</body>

I much prefer using initParams with the ASP <%=Request ... directly in the code than messing with the JavaScript to modify a control within the Silverlight App. After passing it into InitParams, you can load the values into the Resource Dictionary of the application. I go into detail of all of this on my blog post: Pass the IP Address of a User to Silverlight as a Parameter (NOTE: If the link to my website at benmccormack.com doesn't work, you may have to go to the site directly and find it. I posted the write-up on 9/26/2009) .

You cant get the ip address of the client machine using any client side mechansims (javascript,silverlight etc).

Michale Sync posted an interesting article about how to retrieve the client info from silverlight.. silverlight-2-beta1-url-referrer-screen-resolution-clients-data-time-and-ip-address .

this approch should work.. But there is also limitation

It won't be able to get the actual address if the proxy server is hiding those addresses.

Have aa pice of code in your aspx file hosting the silverlight control

var ip = '<%=Request.UserHostAddress%>'

then hookup onload event and set the ip to your silverlight class

function onload() {
            control = document.getElementById(
                    'SilverlightPlugin'
                  );
            iptextblock = control.Content.FindName("txtIP");
            iptextblock.Text = ip;
        }

<object id="SilverlightPlugin"  data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
    <param name="source" value="ClientBin/SilverlightApplication1.xap"/>    
    <param name="onload" value="onload" />

if your silverlight host page is hosted in Apache you try thisan Apache server:

var ip = '<!--#echo var="REMOTE_ADDR"-->';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM