tags:

views:

1264

answers:

3

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?

+1  A: 

Have a a 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"-->';
rravuri
I learned to pass a variable to the Silverlight app. I had to add an element <TextBlock x:Name="txtIP" /> in my XAML to have a place to put the ip address. However, it only returned 127.0.0.1.
Ben McCormack
Are you debugging our app try to host the app on another machine other than the one you are accessing it from
rravuri
Thanks for the suggestion. I just tried that by hosting it on IIS on my work PC. I actually got better results that way. It's showing the "behind the firewall" IP address, but I think this will work for my needs. When I access over the VPN, I get my VPN client address. From my work PC, I get my internal IP. Cool. I should be able to work with this. Much thanks!
Ben McCormack
+1  A: 

Hi,

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.

Cheers

Ramesh Vel

Ramesh Vel
+3  A: 

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&amp;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).

Ben McCormack