views:

1365

answers:

1

So one of the best parts about the new Silverlight 4 beta is that they finally implemented the big missing feature of the networking stack - Network Credentials!

In the below I have a working request setup, but for some reason I get a "security error" when the request comes back - is this because twitter.com rejected my api call or something that I'm missing in code?

It might be good to point out that when I watch this code execute via fiddler it shows that the xml file for cross domain is pulled down successfully, but that is the last request shown by fiddler ...

public void RequestTimelineFromTwitterAPI()
        {
               WebRequest.RegisterPrefix("https://", System.Net.Browser.WebRequestCreator.ClientHttp);

               WebClient myService = new WebClient();
               myService.AllowReadStreamBuffering = true;
               myService.UseDefaultCredentials = false;
               myService.Credentials = new NetworkCredential("username", "password");
               myService.UseDefaultCredentials = false;

               myService.OpenReadCompleted += new OpenReadCompletedEventHandler(TimelineRequestCompleted);
               myService.OpenReadAsync(new Uri("https://twitter.com/statuses/friends_timeline.xml"));
        }

        public void TimelineRequestCompleted(object sender, System.Net.OpenReadCompletedEventArgs e)
        {
            //anytime I query for e.Result I get a security error
        }
+2  A: 

I found 2 issues that caused this request to throw the security exception

1) - In this video by Tim Heuer it turns out my VS2010 w/ Silverlight 4 toolkit installation didn't match the final build so I'm missing the option that shows up in the "out of browser settings" dialog that provides the checkbox for "Require elevated trust when running outside the browser".

In the video listed above Tim checks this so the Silverlight app can talk to the twitter API

But because my application didn't have this option I had to manually edit the xml file so it looked like the below. You can find this xml under properties in the project folder or inside visual studio directly.

<OutOfBrowserSettings ShortName="TrustedNetworkApp Application" EnableGPUAcceleration="False" ShowInstallMenuItem="True">
  <OutOfBrowserSettings.Blurb>TrustedNetworkApp Application on your desktop; at home, at work or on the go.</OutOfBrowserSettings.Blurb>
  <OutOfBrowserSettings.WindowSettings>
    <WindowSettings Title="TrustedNetworkApp Application" Height="480" Width="640" />
  </OutOfBrowserSettings.WindowSettings>
  <OutOfBrowserSettings.SecuritySettings>
    <SecuritySettings ElevatedPermissions="Required" />
  </OutOfBrowserSettings.SecuritySettings>
  <OutOfBrowserSettings.Icons />
</OutOfBrowserSettings>

Notice the **security settings ElevatedPermissions="Required"

After you save this it's equivalent to checking this as Tim did in the video.

2) - as I was watching that video by Tim I noticed that you have to debug this outside of the browser to get it working. So install the app and run it outside the browser. This app now works.

I'll write a short blog post to summarize my experience with the networking stack under the beta and link to it for anyone interested.

Update

I finally wrote a blog post about my experience building an out of browser twitter client using Silverlight 4 if anyone is interested.

Toran Billups